Skip to content

Commit

Permalink
feat: support dragging the crop box when touch point outside the crop…
Browse files Browse the repository at this point in the history
… box (#339)

* feat: support dragging the crop box when touch point outside the crop box
* refactor: rename updateTouchPointIfNeeded to confineTouchPoint
  • Loading branch information
guoyingtao committed Nov 1, 2023
1 parent 4b94612 commit 646a239
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,34 +315,45 @@ final class CropView: UIView {
rotationControlView.handleDeviceRotation()
}

func updateCropBoxFrame(withTouchPoint touchPoint: CGPoint) {
let contentBounds = getContentBounds()
private func confineTouchPoint(_ touchPoint: CGPoint, in rect: CGRect) -> CGPoint {
var confinedPoint = touchPoint

guard contentBounds.contains(touchPoint) else {
return
// Get the frame dimensions
let frameWidth = frame.size.width
let frameHeight = frame.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
}

let imageContainerRect = imageContainer.convert(imageContainer.bounds, to: self)
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
}

return confinedPoint
}

func updateCropBoxFrame(withTouchPoint touchPoint: CGPoint) {
let imageContainerRect = imageContainer.convert(imageContainer.bounds, to: self)
let imageFrame = CGRect(x: cropWorkbenchView.frame.origin.x - cropWorkbenchView.contentOffset.x,
y: cropWorkbenchView.frame.origin.y - cropWorkbenchView.contentOffset.y,
width: imageContainerRect.size.width,
height: imageContainerRect.size.height)

guard imageFrame.contains(touchPoint) else {
return
}

let touchPoint = confineTouchPoint(touchPoint, in: imageFrame)
let contentBounds = getContentBounds()
let cropViewMinimumBoxSize = cropViewConfig.minimumCropBoxSize
let newCropBoxFrame = viewModel.getNewCropBoxFrame(withTouchPoint: touchPoint,
andContentFrame: contentBounds,
aspectRatioLockEnabled: aspectRatioLockEnabled)

guard newCropBoxFrame.width >= cropViewMinimumBoxSize
&& newCropBoxFrame.minX >= contentBounds.minX
&& newCropBoxFrame.maxX <= contentBounds.maxX
&& newCropBoxFrame.height >= cropViewMinimumBoxSize
&& newCropBoxFrame.minY >= contentBounds.minY
&& newCropBoxFrame.maxY <= contentBounds.maxY else {
&& newCropBoxFrame.height >= cropViewMinimumBoxSize else {
return
}

Expand Down

0 comments on commit 646a239

Please sign in to comment.