From 97e5b9dccfe063729cbd092dfff2858c4a47a929 Mon Sep 17 00:00:00 2001 From: Yingtao Guo Date: Sun, 15 Jan 2023 21:04:46 +0800 Subject: [PATCH] fix: fix the issue that sometimes it is hard to move crop box --- Mantis.xcodeproj/project.pbxproj | 28 +++++++++++++++----- Sources/Mantis/CropView/CropView.swift | 25 ++++++++++++----- Sources/Mantis/CropView/ImageContainer.swift | 2 +- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/Mantis.xcodeproj/project.pbxproj b/Mantis.xcodeproj/project.pbxproj index a923695d..74395efe 100644 --- a/Mantis.xcodeproj/project.pbxproj +++ b/Mantis.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 46; + objectVersion = 54; objects = { /* Begin PBXAggregateTarget section */ @@ -509,6 +509,7 @@ /* Begin PBXShellScriptBuildPhase section */ 5F69CC5726BF89F900568B75 /* ShellScript */ = { isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; buildActionMask = 2147483647; files = ( ); @@ -765,7 +766,11 @@ HEADER_SEARCH_PATHS = "$(inherited)"; INFOPLIST_FILE = Mantis.xcodeproj/MantisTests_Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @loader_path/../Frameworks @loader_path/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@loader_path/../Frameworks", + "@loader_path/Frameworks", + ); MACOSX_DEPLOYMENT_TARGET = 10.15; OTHER_CFLAGS = "$(inherited)"; OTHER_LDFLAGS = "$(inherited)"; @@ -791,7 +796,11 @@ HEADER_SEARCH_PATHS = "$(inherited)"; INFOPLIST_FILE = Mantis.xcodeproj/MantisTests_Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @loader_path/../Frameworks @loader_path/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@loader_path/../Frameworks", + "@loader_path/Frameworks", + ); MACOSX_DEPLOYMENT_TARGET = 10.15; OTHER_CFLAGS = "$(inherited)"; OTHER_LDFLAGS = "$(inherited)"; @@ -908,7 +917,8 @@ SDKROOT = iphoneos; SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator"; SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) SWIFT_PACKAGE"; - SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; SWIFT_VERSION = 5.0; USE_HEADERMAP = NO; }; @@ -926,7 +936,10 @@ HEADER_SEARCH_PATHS = "$(inherited)"; INFOPLIST_FILE = Mantis.xcodeproj/Mantis_Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 11.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx", + ); MACOSX_DEPLOYMENT_TARGET = 10.15; MARKETING_VERSION = 2.3.0; OTHER_CFLAGS = "$(inherited)"; @@ -957,7 +970,10 @@ HEADER_SEARCH_PATHS = "$(inherited)"; INFOPLIST_FILE = Mantis.xcodeproj/Mantis_Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 11.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx", + ); MACOSX_DEPLOYMENT_TARGET = 10.15; MARKETING_VERSION = 2.3.0; OTHER_CFLAGS = "$(inherited)"; diff --git a/Sources/Mantis/CropView/CropView.swift b/Sources/Mantis/CropView/CropView.swift index 157a4f75..77a17595 100644 --- a/Sources/Mantis/CropView/CropView.swift +++ b/Sources/Mantis/CropView/CropView.swift @@ -84,7 +84,7 @@ class CropView: UIView { activityIndicator.heightAnchor.constraint(equalToConstant: indicatorSize).isActive = true return activityIndicator - } () + }() deinit { print("CropView deinit.") @@ -321,13 +321,24 @@ class CropView: UIView { } func updateCropBoxFrame(with point: CGPoint) { - let cropViewMinimumBoxSize = cropViewConfig.minimumCropBoxSize - - let contentFrame = getContentBounds() - let newCropBoxFrame = viewModel.getNewCropBoxFrame(with: point, and: contentFrame, aspectRatioLockEnabled: aspectRatioLockEnabled) - let contentBounds = getContentBounds() + guard contentBounds.contains(point) else { + return + } + + let imageFrame = CGRect(x: scrollView.frame.origin.x - scrollView.contentOffset.x, + y: scrollView.frame.origin.y - scrollView.contentOffset.y, + width: imageContainer.frame.width, + height: imageContainer.frame.height) + + guard imageFrame.contains(point) else { + return + } + + let cropViewMinimumBoxSize = cropViewConfig.minimumCropBoxSize + let newCropBoxFrame = viewModel.getNewCropBoxFrame(with: point, and: contentBounds, aspectRatioLockEnabled: aspectRatioLockEnabled) + guard newCropBoxFrame.width >= cropViewMinimumBoxSize && newCropBoxFrame.minX >= contentBounds.minX && newCropBoxFrame.maxX <= contentBounds.maxX @@ -373,7 +384,7 @@ class CropView: UIView { viewModel.cropBoxFrame = CGRect(x: minX, y: minY, width: maxX - minX, height: maxY - minY) } - } + } } // MARK: - Adjust UI diff --git a/Sources/Mantis/CropView/ImageContainer.swift b/Sources/Mantis/CropView/ImageContainer.swift index abc9b8bf..662aef82 100644 --- a/Sources/Mantis/CropView/ImageContainer.swift +++ b/Sources/Mantis/CropView/ImageContainer.swift @@ -35,7 +35,7 @@ class ImageContainer: UIView { imageView.frame = bounds } - func contains(rect: CGRect, fromView view: UIView, tolerance: CGFloat = 1e-6) -> Bool { + func contains(rect: CGRect, fromView view: UIView, tolerance: CGFloat = 0.5) -> Bool { let newRect = view.convert(rect, to: self) let point1 = newRect.origin