Double tap zoom gesture

This commit is contained in:
Justin Mazzocchi 2020-10-21 22:38:37 -07:00
parent 8de227d780
commit cad54a5818
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
2 changed files with 28 additions and 1 deletions

View file

@ -111,4 +111,10 @@ extension ImageNavigationController: UIGestureRecognizerDelegate {
return false
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
gestureRecognizer === barHideOnTapGestureRecognizer
&& (otherGestureRecognizer as? UITapGestureRecognizer)?.numberOfTapsRequired == 2
}
}

View file

@ -38,6 +38,13 @@ class ImageViewController: UIViewController {
scrollView.showsVerticalScrollIndicator = false
scrollView.maximumZoomScale = Self.maximumZoomScale
let doubleTapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(handleDoubleTap(gestureRecognizer:)))
doubleTapGestureRecognizer.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTapGestureRecognizer)
contentView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(contentView)
@ -168,5 +175,19 @@ extension ImageViewController: UIScrollViewDelegate {
}
private extension ImageViewController {
static let maximumZoomScale: CGFloat = 5
static let maximumZoomScale: CGFloat = 4
@objc func handleDoubleTap(gestureRecognizer: UITapGestureRecognizer) {
if scrollView.zoomScale == scrollView.minimumZoomScale {
let width = contentView.frame.size.width / scrollView.maximumZoomScale
let height = contentView.frame.size.height / scrollView.maximumZoomScale
let center = scrollView.convert(gestureRecognizer.location(in: gestureRecognizer.view), from: contentView)
scrollView.zoom(
to: CGRect(x: center.x - (width / 2), y: center.y - (height / 2), width: width, height: height),
animated: true)
} else {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
}
}
}