Skip to content

Commit

Permalink
fix: fix the weird rotation animation issue for alwaysUsingOnePresetF…
Browse files Browse the repository at this point in the history
…ixedRatio (#202)

I removed some old code for solving some bug for alwaysUsingOnePresetFixedRatio. I totally forgot what the bug was but looks it works fine now.
  • Loading branch information
guoyingtao committed Aug 28, 2022
1 parent 31c435c commit 4440568
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
1 change: 1 addition & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class ViewController: UIViewController, CropViewControllerDelegate {
config.cropToolbarConfig.backgroundColor = .white
config.cropToolbarConfig.foregroundColor = .gray
config.cropToolbarConfig.ratioCandidatesShowType = .alwaysShowRatioList
config.presetFixedRatioType = .alwaysUsingOnePresetFixedRatio(ratio: 2.0 / 1.0)

let cropViewController = Mantis.cropViewController(image: image,
config: config)
Expand Down
32 changes: 5 additions & 27 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,7 @@ class CropView: UIView {
rotationDial.setRotationCenter(by: gridOverlayView.center, of: self)

rotationDial.didRotate = { [unowned self] angle in
if self.forceFixedRatio {
let newRadians = self.viewModel.getTotalRadias(by: angle.radians)
self.viewModel.setRotatingStatus(by: CGAngle(radians: newRadians))
} else {
self.viewModel.setRotatingStatus(by: angle)
}
self.viewModel.setRotatingStatus(by: angle)
}

rotationDial.didFinishedRotate = { [unowned self] in
Expand Down Expand Up @@ -366,7 +361,7 @@ class CropView: UIView {
// MARK: - Adjust UI
extension CropView {
private func rotateScrollView() {
let totalRadians = forceFixedRatio ? viewModel.radians : viewModel.getTotalRadians()
let totalRadians = viewModel.getTotalRadians()
scrollView.transform = CGAffineTransform(rotationAngle: totalRadians)

if viewModel.horizontallyFlip {
Expand Down Expand Up @@ -471,7 +466,7 @@ extension CropView {

let newCropBounds = CGRect(x: 0, y: 0, width: viewModel.cropBoxFrame.width * scale, height: viewModel.cropBoxFrame.height * scale)

let radians = forceFixedRatio ? viewModel.radians : viewModel.getTotalRadians()
let radians = viewModel.getTotalRadians()

// calculate the new bounds of scroll view
let newBoundWidth = abs(cos(radians)) * newCropBounds.size.width + abs(sin(radians)) * newCropBounds.size.height
Expand Down Expand Up @@ -698,7 +693,7 @@ extension CropView {
}

func getTotalRadians() -> CGFloat {
return forceFixedRatio ? viewModel.radians : viewModel.getTotalRadians()
return viewModel.getTotalRadians()
}

func crop() -> CropOutput {
Expand Down Expand Up @@ -739,24 +734,7 @@ extension CropView {

func rotateBy90(rotateAngle: CGFloat, completion: @escaping () -> Void = {}) {
viewModel.setDegree90RotatingStatus()
let rorateDuration = 0.25

if forceFixedRatio {
viewModel.setRotatingStatus(by: CGAngle(radians: viewModel.radians))
let angle = CGAngle(radians: rotateAngle + viewModel.radians)

UIView.animate(withDuration: rorateDuration, animations: {
self.viewModel.setRotatingStatus(by: angle)
}, completion: {[weak self] _ in
guard let self = self else { return }
self.viewModel.rotateBy90(rotateAngle: rotateAngle)
self.viewModel.setBetweenOperationStatus()
completion()
})

return
}

let rorateDuration = 0.25
var rect = gridOverlayView.frame
rect.size.width = gridOverlayView.frame.height
rect.size.height = gridOverlayView.frame.width
Expand Down

0 comments on commit 4440568

Please sign in to comment.