Skip to content

Commit

Permalink
fix: fix flip issue for preset transformation (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyingtao committed Nov 1, 2023
1 parent 5dd652d commit 2d06e2e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
22 changes: 12 additions & 10 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,19 @@ class ViewController: UIViewController, CropViewControllerDelegate {

var config = Mantis.Config()

let transform = Transformation(offset: CGPoint(x: 231.66666666666666, y: 439.6666666666667),
rotation: 0.5929909348487854,
scale: 2.841958076098717,
let transform = Transformation(offset: CGPoint(x: 169, y: 152),
rotation: -0.46043267846107483,
scale: 2.129973210831677,
manualZoomed: true,
initialMaskFrame: CGRect(x: 14.0, y: 62.25, width: 347.0, height: 520.5),
maskFrame: CGRect(x: 59.47694524495677, y: 14.0, width: 256.04610951008647, height: 617.0),
cropWorkbenchViewBounds: CGRect(x: 231.66666666666666,
y: 439.6666666666667,
width: 557.1387432741491,
height: 654.7511809035641))

initialMaskFrame: CGRect(x: 14.0, y: 33, width: 402, height: 603),
maskFrame: CGRect(x: 67.90047201716507, y: 14.0, width: 294.19905596566986, height: 641.0),
cropWorkbenchViewBounds: CGRect(x: 169,
y: 152,
width: 548.380489739444,
height: 704.9696330065433),
horizontallyFlipped: true,
verticallyFlipped:false)
config.cropToolbarConfig.toolbarButtonOptions = [.clockwiseRotate, .reset, .ratio, .autoAdjust, .horizontallyFlip]
config.cropViewConfig.presetTransformationType = .presetInfo(info: transform)
config.cropViewConfig.builtInRotationControlViewType = .slideDial()

Expand Down
4 changes: 3 additions & 1 deletion Sources/Mantis/CropData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public typealias Transformation = (
manualZoomed: Bool,
initialMaskFrame: CGRect,
maskFrame: CGRect,
cropWorkbenchViewBounds: CGRect
cropWorkbenchViewBounds: CGRect,
horizontallyFlipped: Bool,
verticallyFlipped: Bool
)

public struct CropRegion: Equatable {
Expand Down
48 changes: 31 additions & 17 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,7 @@ final class CropView: UIView {

// MARK: - Adjust UI
extension CropView {
private func rotateCropWorkbenchView() {
let totalRadians = viewModel.getTotalRadians()
cropWorkbenchView.transform = CGAffineTransform(rotationAngle: totalRadians)

private func flipCropWorkbenchViewIfNeeded() {
if viewModel.horizontallyFlip {
if viewModel.rotationType.isRotateByMultiple180 {
cropWorkbenchView.transform = cropWorkbenchView.transform.scaledBy(x: -1, y: 1)
Expand All @@ -406,7 +403,12 @@ extension CropView {
cropWorkbenchView.transform = cropWorkbenchView.transform.scaledBy(x: -1, y: 1)
}
}

}

private func rotateCropWorkbenchView() {
let totalRadians = viewModel.getTotalRadians()
cropWorkbenchView.transform = CGAffineTransform(rotationAngle: totalRadians)
flipCropWorkbenchViewIfNeeded()
adjustWorkbenchView(by: totalRadians)
}

Expand Down Expand Up @@ -639,7 +641,9 @@ extension CropView {
manualZoomed: manualZoomed,
initialMaskFrame: getInitialCropBoxRect(),
maskFrame: cropAuxiliaryIndicatorView.frame,
cropWorkbenchViewBounds: cropWorkbenchView.bounds
cropWorkbenchViewBounds: cropWorkbenchView.bounds,
horizontallyFlipped: viewModel.horizontallyFlip,
verticallyFlipped: viewModel.verticallyFlip
)
}

Expand Down Expand Up @@ -758,11 +762,8 @@ extension CropView {
func flip() {
flipOddTimes.toggle()

let flipTransform = cropWorkbenchView.transform.scaledBy(x: scaleX, y: scaleY)

let flipTransform = cropWorkbenchView.transform.scaledBy(x: scaleX, y: scaleY)
let coff: CGFloat = flipOddTimes ? 2 : -2

// let coff: CGFloat = viewModel.radians < 0 ? 2 : -2
cropWorkbenchView.transform = flipTransform.rotated(by: coff*viewModel.radians)

viewModel.degrees *= -1
Expand Down Expand Up @@ -931,7 +932,7 @@ extension CropView: CropViewProtocol {
cropWorkbenchView.zoomScale = transformation.scale
cropWorkbenchView.contentOffset = transformation.offset
viewModel.setBetweenOperationStatus()

if transformation.maskFrame != .zero {
viewModel.cropBoxFrame = transformation.maskFrame
}
Expand Down Expand Up @@ -1000,18 +1001,31 @@ extension CropView: CropViewProtocol {

let manualZoomed = (scale != 1.0)
let transformation = Transformation(offset: offset,
rotation: 0,
scale: scale,
manualZoomed: manualZoomed,
initialMaskFrame: .zero,
maskFrame: maskFrame,
cropWorkbenchViewBounds: .zero)
rotation: 0,
scale: scale,
manualZoomed: manualZoomed,
initialMaskFrame: .zero,
maskFrame: maskFrame,
cropWorkbenchViewBounds: .zero,
horizontallyFlipped: viewModel.horizontallyFlip,
verticallyFlipped: viewModel.verticallyFlip)
return transformation
}

func processPresetTransformation(completion: (Transformation) -> Void) {
switch cropViewConfig.presetTransformationType {
case .presetInfo(let transformInfo):
viewModel.horizontallyFlip = transformInfo.horizontallyFlipped
viewModel.verticallyFlip = transformInfo.verticallyFlipped

if transformInfo.horizontallyFlipped {
flipOddTimes.toggle()
}

if transformInfo.verticallyFlipped {
flipOddTimes.toggle()
}

var newTransform = getTransformInfo(byTransformInfo: transformInfo)

// The first transform is just for retrieving the final cropBoxFrame
Expand Down

0 comments on commit 2d06e2e

Please sign in to comment.