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: solve the issue when using normalizedTransform with presetFixedRatioType #98

Merged
merged 11 commits into from
Apr 22, 2021
28 changes: 16 additions & 12 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -440,27 +440,31 @@ extension CropView {
let radians = viewModel.getTotalRadians()

// calculate the new bounds of scroll view
let width = abs(cos(radians)) * newCropBounds.size.width + abs(sin(radians)) * newCropBounds.size.height
let height = abs(sin(radians)) * newCropBounds.size.width + abs(cos(radians)) * newCropBounds.size.height
let newBoundWidth = abs(cos(radians)) * newCropBounds.size.width + abs(sin(radians)) * newCropBounds.size.height
let newBoundHeight = abs(sin(radians)) * newCropBounds.size.width + abs(cos(radians)) * newCropBounds.size.height

// calculate the zoom area of scroll view
var scaleFrame = viewModel.cropBoxFrame
if scaleFrame.width >= scrollView.contentSize.width {
scaleFrame.size.width = scrollView.contentSize.width

let refContentWidth = abs(cos(radians)) * scrollView.contentSize.width + abs(sin(radians)) * scrollView.contentSize.height
let refContentHeight = abs(sin(radians)) * scrollView.contentSize.width + abs(cos(radians)) * scrollView.contentSize.height

if scaleFrame.width >= refContentWidth {
scaleFrame.size.width = refContentWidth
}
if scaleFrame.height >= scrollView.contentSize.height {
scaleFrame.size.height = scrollView.contentSize.height
if scaleFrame.height >= refContentHeight {
scaleFrame.size.height = refContentHeight
}

let contentOffset = scrollView.contentOffset
let contentOffsetCenter = CGPoint(x: (contentOffset.x + scrollView.bounds.width / 2),
y: (contentOffset.y + scrollView.bounds.height / 2))


scrollView.bounds = CGRect(x: 0, y: 0, width: width, height: height)
scrollView.bounds = CGRect(x: 0, y: 0, width: newBoundWidth, height: newBoundHeight)

let newContentOffset = CGPoint(x: (contentOffsetCenter.x - scrollView.bounds.width / 2),
y: (contentOffsetCenter.y - scrollView.bounds.height / 2))
let newContentOffset = CGPoint(x: (contentOffsetCenter.x - newBoundWidth / 2),
y: (contentOffsetCenter.y - newBoundHeight / 2))
scrollView.contentOffset = newContentOffset

let newCropBoxFrame = GeometryHelper.getInscribeRect(fromOutsideRect: contentRect, andInsideRect: viewModel.cropBoxFrame)
Expand Down Expand Up @@ -552,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 @@ -696,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