Skip to content

Commit

Permalink
refactor: code improving (#354)
Browse files Browse the repository at this point in the history
* refactor: simplify updateContentOffset function

* refactor: refactor some code

simplify flipCropWorkbenchViewIfNeeded
add transformScaleBy(xScale: CGFloat, yScale: CGFloat) to CropWorkbenchViewProtocol
  • Loading branch information
guoyingtao authored Nov 16, 2023
1 parent 5e125f0 commit 11a4474
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ struct CropBoxLockedAspectFrameUpdater {
case .bottomRight:
handleBottomRightEdgeFrameUpdate()
default:
print("none")
break
}
}

Expand Down
32 changes: 13 additions & 19 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ final class CropView: UIView {
}

private func imageStatusChanged() -> Bool {
if viewModel.getTotalRadians() != 0 { return true }
if viewModel.getTotalRadians() != 0 {
return true
}

if forceFixedRatio {
if checkForForceFixedRatioFlag {
Expand Down Expand Up @@ -363,8 +365,6 @@ final class CropView: UIView {
andContentFrame: contentBounds,
aspectRatioLockEnabled: aspectRatioLockEnabled)

print("newCropBoxFrame is \(newCropBoxFrame.width) - \(newCropBoxFrame.height)")

guard newCropBoxFrame.width >= cropViewMinimumBoxSize
&& newCropBoxFrame.height >= cropViewMinimumBoxSize else {
return
Expand Down Expand Up @@ -417,19 +417,13 @@ final class CropView: UIView {
extension CropView {
private func flipCropWorkbenchViewIfNeeded() {
if viewModel.horizontallyFlip {
if viewModel.rotationType.isRotateByMultiple180 {
cropWorkbenchView.transform = cropWorkbenchView.transform.scaledBy(x: -1, y: 1)
} else {
cropWorkbenchView.transform = cropWorkbenchView.transform.scaledBy(x: 1, y: -1)
}
let scale: CGFloat = viewModel.rotationType.isRotatedByMultiple180 ? -1 : 1
cropWorkbenchView.transformScaleBy(xScale: scale, yScale: -scale)
}

if viewModel.verticallyFlip {
if viewModel.rotationType.isRotateByMultiple180 {
cropWorkbenchView.transform = cropWorkbenchView.transform.scaledBy(x: 1, y: -1)
} else {
cropWorkbenchView.transform = cropWorkbenchView.transform.scaledBy(x: -1, y: 1)
}
let scale: CGFloat = viewModel.rotationType.isRotatedByMultiple180 ? 1 : -1
cropWorkbenchView.transformScaleBy(xScale: scale, yScale: -scale)
}
}

Expand Down Expand Up @@ -624,7 +618,7 @@ extension CropView {
let height = abs(sin(radians)) * cropAuxiliaryIndicatorView.frame.width + abs(cos(radians)) * cropAuxiliaryIndicatorView.frame.height

let newSize: CGSize
if viewModel.rotationType.isRotateByMultiple180 {
if viewModel.rotationType.isRotatedByMultiple180 {
newSize = CGSize(width: width, height: height)
} else {
newSize = CGSize(width: height, height: width)
Expand Down Expand Up @@ -774,13 +768,13 @@ extension CropView {
var scaleY: CGFloat = 1

if isHorizontal {
if viewModel.rotationType.isRotateByMultiple180 {
if viewModel.rotationType.isRotatedByMultiple180 {
scaleX = -scaleX
} else {
scaleY = -scaleY
}
} else {
if viewModel.rotationType.isRotateByMultiple180 {
if viewModel.rotationType.isRotatedByMultiple180 {
scaleY = -scaleY
} else {
scaleX = -scaleX
Expand Down Expand Up @@ -829,7 +823,7 @@ extension CropView: CropViewProtocol {
}

func getImageHorizontalToVerticalRatio() -> Double {
if viewModel.rotationType.isRotateByMultiple180 {
if viewModel.rotationType.isRotatedByMultiple180 {
return Double(image.horizontalToVerticalRatio())
} else {
return Double(1 / image.horizontalToVerticalRatio())
Expand Down Expand Up @@ -1144,15 +1138,15 @@ extension CropView: CropViewProtocol {
var scaleY = cropWorkbenchView.zoomScale

if viewModel.horizontallyFlip {
if viewModel.rotationType.isRotateByMultiple180 {
if viewModel.rotationType.isRotatedByMultiple180 {
scaleX = -scaleX
} else {
scaleY = -scaleY
}
}

if viewModel.verticallyFlip {
if viewModel.rotationType.isRotateByMultiple180 {
if viewModel.rotationType.isRotatedByMultiple180 {
scaleY = -scaleY
} else {
scaleX = -scaleX
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mantis/CropView/CropViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ enum ImageRotationType: CGFloat {
}
}

var isRotateByMultiple180: Bool {
var isRotatedByMultiple180: Bool {
return self == .none || self == .counterclockwise180
}
}
Expand Down
12 changes: 3 additions & 9 deletions Sources/Mantis/CropView/CropWorkbenchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,9 @@ final class CropWorkbenchView: UIScrollView {
extension CropWorkbenchView: CropWorkbenchViewProtocol {
func updateContentOffset() {
contentOffset.x = max(contentOffset.x, 0)
contentOffset.y = max(contentOffset.y, 0)

if contentSize.height - contentOffset.y <= bounds.size.height {
contentOffset.y = contentSize.height - bounds.size.height
}

if contentSize.width - contentOffset.x <= bounds.size.width {
contentOffset.x = contentSize.width - bounds.size.width
}
contentOffset.y = max(contentOffset.y, 0)
contentOffset.x = min(contentOffset.x, contentSize.width - bounds.size.width)
contentOffset.y = min(contentOffset.y, contentSize.height - bounds.size.height)
}

func updateMinZoomScale() {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mantis/CropViewController/CropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ open class CropViewController: UIViewController {
case .circle, .square, .heart:
self.config.presetFixedRatioType = .alwaysUsingOnePresetFixedRatio(ratio: 1)
default:
()
break
}

super.init(nibName: nil, bundle: nil)
Expand Down
7 changes: 7 additions & 0 deletions Sources/Mantis/Protocols/CropScrollViewProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ protocol CropWorkbenchViewProtocol: UIScrollView {
func updateLayout(byNewSize newSize: CGSize)
func reset(by rect: CGRect)
func resetImageContent(by cropBoxFrame: CGRect)
func transformScaleBy(xScale: CGFloat, yScale: CGFloat)
}

extension CropWorkbenchViewProtocol {
func transformScaleBy(xScale: CGFloat, yScale: CGFloat) {
transform = transform.scaledBy(x: xScale, y: yScale)
}
}

0 comments on commit 11a4474

Please sign in to comment.