Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix orientation issues on iPad #281

Merged
merged 2 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example/CustomizedCropToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class CustomizedCropToolbar: UIView, CropToolbarProtocol {
}

func adjustLayoutWhenOrientationChange() {
if Orientation.isPortrait {
if Orientation.treatAsPortrait {
stackView?.axis = .horizontal
} else {
stackView?.axis = .vertical
Expand All @@ -69,7 +69,7 @@ class CustomizedCropToolbar: UIView, CropToolbarProtocol {
public override var intrinsicContentSize: CGSize {
let superSize = super.intrinsicContentSize

if Orientation.isPortrait {
if Orientation.treatAsPortrait {
return CGSize(width: superSize.width, height: config.heightForVerticalOrientation)
} else {
return CGSize(width: config.widthForHorizontalOrientation, height: superSize.height)
Expand Down
4 changes: 2 additions & 2 deletions Example/CustomizedCropToolbarWithoutList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ class CustomizedCropToolbarWithoutList: UIView, CropToolbarProtocol {
public override var intrinsicContentSize: CGSize {
let superSize = super.intrinsicContentSize

if Orientation.isPortrait {
if Orientation.treatAsPortrait {
return CGSize(width: superSize.width, height: config.heightForVerticalOrientation)
} else {
return CGSize(width: config.widthForHorizontalOrientation, height: superSize.height)
}
}

func adjustLayoutWhenOrientationChange() {
if Orientation.isPortrait {
if Orientation.treatAsPortrait {
stackView?.axis = .horizontal
} else {
stackView?.axis = .vertical
Expand Down
8 changes: 4 additions & 4 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class CropView: UIView {
private func adaptAngleDashboardToCropBox() {
guard let rotationDial = rotationDial else { return }

if Orientation.isPortrait {
if Orientation.treatAsPortrait {
rotationDial.transform = CGAffineTransform(rotationAngle: 0)
rotationDial.frame.origin.x = cropAuxiliaryIndicatorView.frame.origin.x +
(cropAuxiliaryIndicatorView.frame.width - rotationDial.frame.width) / 2
Expand Down Expand Up @@ -404,7 +404,7 @@ extension CropView {
let rect = self.bounds
var contentRect = CGRect.zero

if Orientation.isPortrait {
if Orientation.treatAsPortrait {
contentRect.origin.x = rect.origin.x + cropViewPadding
contentRect.origin.y = rect.origin.y + cropViewPadding

Expand Down Expand Up @@ -753,12 +753,12 @@ extension CropView: CropViewProtocol {
}
}

func prepareForDeviceRotation() {
func prepareForViewWillTransition() {
viewModel.setDegree90RotatingStatus()
saveAnchorPoints()
}

func handleDeviceRotated() {
func handleViewWillTransition() {
viewModel.resetCropFrame(by: getInitialCropBoxRect())

cropWorkbenchView.transform = CGAffineTransform(scaleX: 1, y: 1)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Mantis/CropViewController/CropToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public class CropToolbar: UIView, CropToolbarProtocol {
public override var intrinsicContentSize: CGSize {
let superSize = super.intrinsicContentSize

if Orientation.isPortrait {
if Orientation.treatAsPortrait {
return CGSize(width: superSize.width, height: config.heightForVerticalOrientation)
} else {
return CGSize(width: config.widthForHorizontalOrientation, height: superSize.height)
Expand All @@ -158,7 +158,7 @@ public class CropToolbar: UIView, CropToolbarProtocol {
}

public func adjustLayoutWhenOrientationChange() {
if Orientation.isPortrait {
if Orientation.treatAsPortrait {
optionButtonStackView?.axis = .horizontal
optionButtonStackView?.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
} else {
Expand Down
20 changes: 8 additions & 12 deletions Sources/Mantis/CropViewController/CropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ open class CropViewController: UIViewController {
public weak var delegate: CropViewControllerDelegate?
public var config = Mantis.Config()

private var orientation: UIInterfaceOrientation = .unknown

var cropView: CropViewProtocol!
var cropToolbar: CropToolbarProtocol!

Expand Down Expand Up @@ -153,19 +151,17 @@ open class CropViewController: UIViewController {
return [.top, .bottom]
}

// It is triggered by (1) - device rotation or (2) - split view operations on iPad
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
cropView.prepareForDeviceRotation()
handleDeviceRotated()
}
cropView.prepareForViewWillTransition()
handleViewWillTransition()
}

@objc func handleDeviceRotated() {
@objc func handleViewWillTransition() {
let currentOrientation = Orientation.interfaceOrientation

guard currentOrientation != .unknown else { return }
guard currentOrientation != orientation else { return }

orientation = currentOrientation

if UIDevice.current.userInterfaceIdiom == .phone
&& currentOrientation == .portraitUpsideDown {
Expand All @@ -180,7 +176,7 @@ open class CropViewController: UIViewController {
// So delay the execution to make sure handleRotate runs after the final
// viewDidLayoutSubviews
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
self?.cropView.handleDeviceRotated()
self?.cropView.handleViewWillTransition()
}
}

Expand Down Expand Up @@ -321,7 +317,7 @@ extension CropViewController {
}

private func setStackViewAxis() {
if Orientation.isPortrait {
if Orientation.treatAsPortrait {
stackView?.axis = .vertical
} else if Orientation.isLandscape {
stackView?.axis = .horizontal
Expand All @@ -338,7 +334,7 @@ extension CropViewController {
stackView?.removeArrangedSubview(cropStackView)
stackView?.removeArrangedSubview(cropToolbar)

if Orientation.isPortrait || Orientation.isLandscapeRight {
if Orientation.treatAsPortrait || Orientation.isLandscapeRight {
stackView?.addArrangedSubview(cropStackView)
stackView?.addArrangedSubview(cropToolbar)
} else if Orientation.isLandscapeLeft {
Expand Down
42 changes: 17 additions & 25 deletions Sources/Mantis/Helpers/Orientation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,42 @@ public struct Orientation {
return application.statusBarOrientation
}
}

static var deviceOrientation: UIDeviceOrientation? {
device.orientation.isValidInterfaceOrientation
? device.orientation
: nil
}


private static var application: UIApplication { .shared }
private static var device: UIDevice { .current }

/**
Whether or not the device is in landscape orientation.
Whether or not the interface is in landscape orientation.
*/
public static var isLandscape: Bool {
device.orientation.isValidInterfaceOrientation
? device.orientation.isLandscape
: interfaceOrientation.isLandscape

interfaceOrientation.isLandscape
}

/**
Whether or not the device is in landscape left orientation.
Whether or not the interface is in landscape left orientation.
*/
public static var isLandscapeLeft: Bool {
device.orientation.isValidInterfaceOrientation
? device.orientation == .landscapeLeft
: interfaceOrientation == .landscapeLeft
interfaceOrientation == .landscapeLeft
}

/**
Whether or not the device is in landscape right orientation.
Whether or not the interface is in landscape right orientation.
*/
public static var isLandscapeRight: Bool {
device.orientation.isValidInterfaceOrientation
? device.orientation == .landscapeRight
: interfaceOrientation == .landscapeRight
interfaceOrientation == .landscapeRight
}

/**
Whether or not the device is in portrait orientation.
Whether or not the interface is in portrait orientation.
*/
public static var isPortrait: Bool {
device.orientation.isValidInterfaceOrientation
? device.orientation.isPortrait
: interfaceOrientation.isPortrait
interfaceOrientation.isPortrait
}

/**
Whether or not the interface is treated as in portrait orientation.
For devices other than iPhone, they have enough space for landscape orientation, so we can always use portait layout for them.
*/
public static var treatAsPortrait: Bool {
interfaceOrientation.isPortrait || UIDevice.current.userInterfaceIdiom != .phone
}
}
2 changes: 1 addition & 1 deletion Sources/Mantis/Protocols/CropToolbarProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public extension CropToolbarProtocol {
let highPriority: Float = 10000
let lowPriority: Float = 1

if Orientation.isPortrait {
if Orientation.treatAsPortrait {
setContentHuggingPriority(UILayoutPriority(highPriority), for: .vertical)
setContentCompressionResistancePriority(UILayoutPriority(highPriority), for: .vertical)
setContentHuggingPriority(UILayoutPriority(lowPriority), for: .horizontal)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Mantis/Protocols/CropViewProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ protocol CropViewProtocol: UIView {
func getRatioType(byImageIsOriginalisHorizontal isHorizontal: Bool) -> RatioType
func getImageHorizontalToVerticalRatio() -> Double
func resetComponents()
func prepareForDeviceRotation()
func handleDeviceRotated()
func prepareForViewWillTransition()
func handleViewWillTransition()
func setFixedRatio(_ ratio: Double, zoom: Bool, presetFixedRatioType: PresetFixedRatioType)
func rotateBy90(withRotateType rotateType: RotateBy90DegreeType, completion: @escaping () -> Void)
func handleAlterCropper90Degree()
Expand Down