I have a PDFView which in itself has quite a lot of gestures already. In that PDFView is an Image. I added a PanGesture so I can drag it around. It got mixed up with the scrolling of the PDF Page itself when zoomed in. So I overwrote the gestureRecognizer shouldRequireFailureOf Delegate Method and everything was fine.
Now comes the tricky part I need help with: The user should now be able to long press the image to enable resizing. When enabled I want them to use a pinch gesture to shrink or enlarge the image.
@objc func longPressed(_ sender: UILongPressGestureRecognizer) {
pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinched(_:)))
pdfContainer.addGestureRecognizer(zoomGesture!)
}
Since the pinch should normally not do anything I added it at the longPress and deleted it after the pinch:
@objc func pinched(_ sender: UIPinchGestureRecognizer) {
guard let annotation = imageAnnotation else { return }
switch sender.state {
case .began:
initalBounds = annotation.bounds
case .changed:
guard let inital = initalBounds else { return }
let newWidth: CGFloat = inital.width * sender.scale
let newHeight: CGFloat = inital.height * sender.scale
annotation.bounds = CGRect(x: inital.midX, y: inital.midY, width: newWidth, height: newHeight)
default:
initalBounds = nil
pdfContainer.removeGestureRecognizer(pinchGesture!)
}
}
Before the longPress everything works as it should. After the longPress the panGesture doesn't work any longer so does the pinch (not working). Since that might also be due to my delegate methods (only pan & long press have their delegates declared; pinch not) I add their code here, too:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
var touchedAnnotation = true
let touchedLocation: CGPoint = touch.location(in: pdfContainer)
if let page = pdfContainer.page(for: touchedLocation, nearest: true), let annotation = imageAnnotation {
let convertedLocation: CGPoint = pdfContainer.convert(touchedLocation, to: page)
if convertedLocation.x < (annotation.bounds.minX - DRAG_RECOGNITION_DISTANCE) { touchedAnnotation = false }
else if convertedLocation.y < (annotation.bounds.minY - DRAG_RECOGNITION_DISTANCE) { touchedAnnotation = false }
else if convertedLocation.x > (annotation.bounds.maxY + DRAG_RECOGNITION_DISTANCE) { touchedAnnotation = false }
else if convertedLocation.y > (annotation.bounds.maxY + DRAG_RECOGNITION_DISTANCE) { touchedAnnotation = false }
}
return touchedAnnotation
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return otherGestureRecognizer == dragGesture || otherGestureRecognizer == longPressGesture || otherGestureRecognizer == pinchGesture
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return gestureRecognizer == dragGesture || gestureRecognizer == longPressGesture || gestureRecognizer == pinchGesture
}
I searched for a similar question, but only found someone trying to disable a swipe back gesture. Which got them a very specific answer I couldn't use much. It's quite a long question... sorry. I really would love some help though.