@objc func handlePinchGesture(_ recognizer: UIPinchGestureRecognizer) {
guard isScalingEnabled, let targetView = self.scalingView else { return }
if recognizer.state == .began {
previousScale = 1.0
initialPinchLocation = recognizer.location(in: targetView)
}
if recognizer.state == .ended || recognizer.state == .cancelled {
if hasRotationApplied {
gestureDelegate?.applyScalingAndRedraw(for: targetView, axis: lastScaleAxis, scaleFactor: finalScaleValue)
} else {
gestureDelegate?.finalizeSnap(for: targetView, usingPoints: originalPoints)
}
return
}
if recognizer.numberOfTouches < 2 {
initialPinchLocation = recognizer.location(in: targetView)
return
}
let touchPointA = recognizer.location(ofTouch: 0, in: targetView)
let touchPointB = recognizer.location(ofTouch: 1, in: targetView)
lastScaleAxis = detectScaleAxis(from: touchPointA, to: touchPointB, basedOn: originalPoints)
let scaleFactor = recognizer.scale / previousScale
let xDiff = originalPoints[1].x - originalPoints[0].x
let yDiff = originalPoints[1].y - originalPoints[0].y
if xDiff == 0 || yDiff == 0 {
hasRotationApplied = false
switch detectGestureDirection(touchPointA, point2: touchPointB) {
case "V":
targetView.transform = targetView.transform.scaledBy(x: 1, y: scaleFactor)
case "H":
targetView.transform = targetView.transform.scaledBy(x: scaleFactor, y: 1)
default:
targetView.transform = targetView.transform.scaledBy(x: scaleFactor, y: scaleFactor)
}
previousScale = recognizer.scale
} else {
finalScaleValue = recognizer.scale
hasRotationApplied = true
targetView.transform = targetView.transform.scaledBy(x: scaleFactor, y: scaleFactor)
}
previousScale = recognizer.scale
}
< /code>
func applyScalingAndRedraw(for view: UIView, axis: String, scaleFactor: CGFloat) {
// Computes new points based on the scaling axis and applies redraw logic
// Geometry methods update coordinates and redraw the rectangle correctly
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... mapbox-wit
Мобильная версия