Skip to content

Commit

Permalink
fix: fix wrong fix ratios when dragging CropBox outside (#348)
Browse files Browse the repository at this point in the history
loose dragging outside restrictions for fix ratios
  • Loading branch information
guoyingtao authored Nov 2, 2023
1 parent 15834d9 commit 2d5c9ac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
6 changes: 2 additions & 4 deletions Sources/Mantis/CropView/CropBoxLockedAspectFrameUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,27 @@ struct CropBoxLockedAspectFrameUpdater {

func handleLeftEdgeFrameUpdate() {
updateHeightFromBothSides()
xDelta = max(0, xDelta)
cropBoxFrame.origin.x = cropOriginFrame.origin.x + xDelta
cropBoxFrame.size.width = cropOriginFrame.width - xDelta
cropBoxFrame.size.height = cropBoxFrame.size.width / aspectRatio
}

func handleRightEdgeFrameUpdate() {
updateHeightFromBothSides()
cropBoxFrame.size.width = min(cropOriginFrame.width + xDelta, contentFrame.height * aspectRatio)
cropBoxFrame.size.width = cropOriginFrame.width + xDelta
cropBoxFrame.size.height = cropBoxFrame.size.width / aspectRatio
}

func handleTopEdgeFrameUpdate() {
updateWidthFromBothSides()
yDelta = max(0, yDelta)
cropBoxFrame.origin.y = cropOriginFrame.origin.y + yDelta
cropBoxFrame.size.height = cropOriginFrame.height - yDelta
cropBoxFrame.size.width = cropBoxFrame.size.height * aspectRatio
}

func handleBottomEdgeFrameUpdate() {
updateWidthFromBothSides()
cropBoxFrame.size.height = min(cropOriginFrame.height + yDelta, contentFrame.width / aspectRatio)
cropBoxFrame.size.height = cropOriginFrame.height + yDelta
cropBoxFrame.size.width = cropBoxFrame.size.height * aspectRatio
}

Expand Down
26 changes: 16 additions & 10 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,20 +319,20 @@ final class CropView: UIView {
var confinedPoint = touchPoint

// Get the frame dimensions
let frameWidth = frame.size.width
let frameHeight = frame.size.height
let rectWidth = rect.size.width
let rectHeight = rect.size.height

// Check if the touch point is outside the frame
if touchPoint.x < frame.origin.x {
confinedPoint.x = frame.origin.x
} else if touchPoint.x > (frame.origin.x + frameWidth) {
confinedPoint.x = frame.origin.x + frameWidth
if touchPoint.x < rect.origin.x {
confinedPoint.x = rect.origin.x
} else if touchPoint.x > (rect.origin.x + rectWidth) {
confinedPoint.x = rect.origin.x + rectWidth
}

if touchPoint.y < frame.origin.y {
confinedPoint.y = frame.origin.y
} else if touchPoint.y > (frame.origin.y + frameHeight) {
confinedPoint.y = frame.origin.y + frameHeight
if touchPoint.y < rect.origin.y {
confinedPoint.y = rect.origin.y
} else if touchPoint.y > (rect.origin.y + rectHeight) {
confinedPoint.y = rect.origin.y + rectHeight
}

return confinedPoint
Expand All @@ -352,6 +352,8 @@ 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 All @@ -360,6 +362,10 @@ final class CropView: UIView {
if imageContainer.contains(rect: newCropBoxFrame, fromView: self, tolerance: 0.5) {
viewModel.cropBoxFrame = newCropBoxFrame
} else {
if aspectRatioLockEnabled {
return
}

let minX = max(viewModel.cropBoxFrame.minX, newCropBoxFrame.minX)
let minY = max(viewModel.cropBoxFrame.minY, newCropBoxFrame.minY)
let maxX = min(viewModel.cropBoxFrame.maxX, newCropBoxFrame.maxX)
Expand Down

0 comments on commit 2d5c9ac

Please sign in to comment.