Skip to content

Commit

Permalink
fix: solve the issues when using normalizedTransform with presetFixed…
Browse files Browse the repository at this point in the history
…RatioType (#98)
  • Loading branch information
guoyingtao committed Apr 22, 2021
1 parent 5e8eb20 commit de03623
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ extension CropView {
rotation: getTotalRadians(),
scale: scrollView.zoomScale,
manualZoomed: manualZoomed,
intialMaskFrame: viewModel.cropOrignFrame,
intialMaskFrame: getInitialCropBoxRect(),
maskFrame: gridOverlayView.frame,
scrollBounds: scrollView.bounds
)
Expand Down Expand Up @@ -700,9 +700,9 @@ extension CropView {
rotationDial?.rotateDialPlate(to: CGAngle(radians: radians), animated: false)
}


func setFixedRatioCropBox(zoom: Bool = true) {
viewModel.setCropBoxFrame(by: getInitialCropBoxRect(),
and: getImageRatioH())
viewModel.setCropBoxFrame(by: getInitialCropBoxRect(), and: getImageRatioH())

let contentRect = getContentBounds()
adjustUIForNewCrop(contentRect: contentRect, animation: false, zoom: zoom) { [weak self] in
Expand Down
4 changes: 2 additions & 2 deletions Sources/Mantis/CropView/CropViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ class CropViewModel: NSObject {
return newCropBoxFrame
}

func setCropBoxFrame(by initialCropBox: CGRect, and imageRationH: Double) {
var cropBoxFrame = initialCropBox
func setCropBoxFrame(by refCropBox: CGRect, and imageRationH: Double) {
var cropBoxFrame = refCropBox
let center = cropBoxFrame.center

if (aspectRatio > CGFloat(imageRationH)) {
Expand Down
24 changes: 12 additions & 12 deletions Sources/Mantis/CropViewController/CropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ public class CropViewController: UIViewController {
switch(config.presetFixedRatioType) {
case .alwaysUsingOnePresetFixedRatio(let ratio):
config.cropToolbarConfig.includeFixedRatioSettingButton = false
setFixedRatio(ratio)

if case .none = config.presetTransformationType {
setFixedRatio(ratio)
}

case .canUseMultiplePresetFixedRatio(let defaultRatio):
if (defaultRatio > 0) {
setFixedRatio(defaultRatio)
Expand Down Expand Up @@ -213,8 +217,7 @@ public class CropViewController: UIViewController {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
self?.cropView.handleRotate()
}
}

}

private func setFixedRatio(_ ratio: Double, zoom: Bool = true) {
cropToolbar.handleFixedRatioSetted(ratio: ratio)
Expand Down Expand Up @@ -250,7 +253,7 @@ public class CropViewController: UIViewController {
}
}

private func processPresetTransformation(completion: ()->Void) {
private func processPresetTransformation(completion: (Transformation)->Void) {
if case .presetInfo(let transformInfo) = config.presetTransformationType {
var newTransform = getTransformInfo(byTransformInfo: transformInfo)

Expand All @@ -261,25 +264,22 @@ public class CropViewController: UIViewController {
let adjustScale = (cropView.viewModel.cropBoxFrame.width / cropView.viewModel.cropOrignFrame.width) / (transformInfo.maskFrame.width / transformInfo.intialMaskFrame.width)
newTransform.scale *= adjustScale
cropView.transform(byTransformInfo: newTransform)
completion()
completion(transformInfo)
} else if case .presetNormalizedInfo(let normailizedInfo) = config.presetTransformationType {
let transformInfo = getTransformInfo(byNormalizedInfo: normailizedInfo);
cropView.transform(byTransformInfo: transformInfo)
cropView.scrollView.frame = transformInfo.maskFrame
completion()
completion(transformInfo)
}
}

public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

processPresetTransformation { [weak self] in
processPresetTransformation() { [weak self] transform in
guard let self = self else { return }

// Preset transformation changed preset fixed ratio crop box
// So we need to reset it again.
if case .alwaysUsingOnePresetFixedRatio = self.config.presetFixedRatioType {
self.cropView.setFixedRatioCropBox()
self.cropView.aspectRatioLockEnabled = true
self.cropView.viewModel.aspectRatio = transform.maskFrame.width / transform.maskFrame.height
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mantis/Mantis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public enum PresetTransformationType {
public enum PresetFixedRatioType {
/** When choose alwaysUsingOnePresetFixedRatio, fixed-ratio setting button does not show.
*/
case alwaysUsingOnePresetFixedRatio(ratio: Double)
case alwaysUsingOnePresetFixedRatio(ratio: Double = 0)
case canUseMultiplePresetFixedRatio(defaultRatio: Double = 0)
}

Expand Down

0 comments on commit de03623

Please sign in to comment.