Skip to content

Commit

Permalink
fix: solve the issue of initial minimum zoom scale is not working (#190)
Browse files Browse the repository at this point in the history
* fix: solve the issue of initial minimum zoom scale is not working

* chore:  document that minimumZoomScale needs to be >= 1.0

remove commented #  pod 'Mantis', '~> 2.0.1'
  • Loading branch information
guoyingtao committed Jul 28, 2022
1 parent c958d6f commit 560e272
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Example/MantisExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
5FC10B0D217A9EDF00582874 /* Sources */,
5FC10B0E217A9EDF00582874 /* Frameworks */,
5FC10B0F217A9EDF00582874 /* Resources */,
5B5D9B6DDD60E517CAAD07B0 /* [CP] Embed Pods Frameworks */,
800F1564269B4B40CE61575F /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -264,7 +264,7 @@
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
5B5D9B6DDD60E517CAAD07B0 /* [CP] Embed Pods Frameworks */ = {
800F1564269B4B40CE61575F /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
Expand Down
4 changes: 3 additions & 1 deletion Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ class ViewController: UIViewController, CropViewControllerDelegate {

var config = Mantis.Config()
config.showAttachedCropToolbar = false
config.cropViewConfig.showRotationDial = false
config.cropViewConfig.showRotationDial = true
config.cropViewConfig.minimumZoomScale = 2.0
config.cropViewConfig.maximumZoomScale = 10.0

let cropToolbar = MyNavigationCropToolbar(frame: .zero)
let cropViewController = Mantis.cropViewController(image: image, config: config, cropToolbar: cropToolbar)
Expand Down
23 changes: 15 additions & 8 deletions Sources/Mantis/CropView/CropScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ class CropScrollView: UIScrollView {
var touchesCancelled = {}
var touchesEnded = {}

override init(frame: CGRect) {
private var initialMinimumZoomScale: CGFloat = 1.0

init(frame: CGRect, minimumZoomScale: CGFloat = 1.0, maximumZoomScale: CGFloat = 15.0) {
super.init(frame: frame)
alwaysBounceHorizontal = true
alwaysBounceVertical = true
showsHorizontalScrollIndicator = false
showsVerticalScrollIndicator = false
contentInsetAdjustmentBehavior = .never
minimumZoomScale = 1.0
maximumZoomScale = 15.0
self.minimumZoomScale = minimumZoomScale
self.maximumZoomScale = maximumZoomScale
initialMinimumZoomScale = minimumZoomScale
clipsToBounds = false
contentSize = bounds.size
layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
Expand Down Expand Up @@ -70,7 +73,7 @@ class CropScrollView: UIScrollView {
let scaleW = bounds.width / imageContainer.bounds.width
let scaleH = bounds.height / imageContainer.bounds.height

return max(scaleW, scaleH)
return max(scaleW, scaleH) * initialMinimumZoomScale
}

func updateMinZoomScale() {
Expand Down Expand Up @@ -104,10 +107,14 @@ class CropScrollView: UIScrollView {

func resetBy(rect: CGRect) {
// Reseting zoom need to be before resetting frame and contentsize
minimumZoomScale = 1.0
zoomScale = 1.0

minimumZoomScale = max(1.0, initialMinimumZoomScale)
zoomScale = minimumZoomScale

let newRect = CGRect(x: rect.origin.x,
y: rect.origin.y,
width: rect.width * minimumZoomScale,
height: rect.height * minimumZoomScale)
frame = rect
contentSize = rect.size
contentSize = newRect.size
}
}
15 changes: 10 additions & 5 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class CropView: UIView {
let gridOverlayView: CropOverlayView
var rotationDial: RotationDial?

lazy var scrollView = CropScrollView(frame: bounds)
lazy var scrollView = CropScrollView(frame: bounds,
minimumZoomScale: cropViewConfig.minimumZoomScale,
maximumZoomScale: cropViewConfig.maximumZoomScale)
lazy var cropMaskViewManager = CropMaskViewManager(with: self,
cropRatio: CGFloat(getImageRatioH()),
cropShapeType: cropViewConfig.cropShapeType,
Expand Down Expand Up @@ -198,6 +200,10 @@ class CropView: UIView {
scrollView.minimumZoomScale = cropViewConfig.minimumZoomScale
scrollView.maximumZoomScale = cropViewConfig.maximumZoomScale

if cropViewConfig.minimumZoomScale > 1 {
scrollView.zoomScale = cropViewConfig.minimumZoomScale
}

setGridOverlayView()
}

Expand All @@ -209,8 +215,8 @@ class CropView: UIView {
scrollView.transform = .identity
scrollView.resetBy(rect: viewModel.cropBoxFrame)

imageContainer.frame = scrollView.bounds
imageContainer.center = CGPoint(x: scrollView.bounds.width/2, y: scrollView.bounds.height/2)
imageContainer.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)
scrollView.contentOffset = CGPoint(x: (imageContainer.frame.width - scrollView.frame.width) / 2, y: (imageContainer.frame.height - scrollView.frame.height) / 2)

gridOverlayView.superview?.bringSubviewToFront(gridOverlayView)

Expand Down Expand Up @@ -518,7 +524,7 @@ extension CropView {
scrollView.updateLayout(byNewSize: CGSize(width: width, height: height))

if !manualZoomed || scrollView.shouldScale() {
scrollView.zoomScaleToBound()
scrollView.zoomScaleToBound(animated: false)
manualZoomed = false
} else {
scrollView.updateMinZoomScale()
Expand Down Expand Up @@ -557,7 +563,6 @@ extension CropView {
// MARK: - internal API
extension CropView {
func crop(_ image: UIImage) -> CropOutput {

let cropInfo = getCropInfo()

let transformation = Transformation(
Expand Down
1 change: 1 addition & 0 deletions Sources/Mantis/CropViewConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public struct CropViewConfig {

public var presetTransformationType: PresetTransformationType = .none

/// minimumZoomScale must be no less than 1
public var minimumZoomScale: CGFloat = 1

public var maximumZoomScale: CGFloat = 15
Expand Down

0 comments on commit 560e272

Please sign in to comment.