Skip to content

Instantly share code, notes, and snippets.

@fl034
Forked from gunantosteven/doubleTapZoom
Last active March 4, 2025 10:32
Show Gist options
  • Save fl034/fda2e8140e9999df60166595d322ae13 to your computer and use it in GitHub Desktop.
Save fl034/fda2e8140e9999df60166595d322ae13 to your computer and use it in GitHub Desktop.
Double Tap Zoom to The Tap Location Swift 5 iOS 13
@objc func handleDoubleTap(_ recognizer: UITapGestureRecognizer) {
let scale = min(scrollView.zoomScale * 2, scrollView.maximumZoomScale)
if scrollView.zoomScale == scrollView.minimumZoomScale { // zoom in
let point = recognizer.location(in: imageView)
let scrollSize = scrollView.frame.size
let zoomScale = scrollView.maximumZoomScale * 0.7
let size = CGSize(width: scrollSize.width / zoomScale,
height: scrollSize.height / zoomScale)
let origin = CGPoint(x: point.x - size.width / 2,
y: point.y - size.height / 2)
scrollView.zoom(to:CGRect(origin: origin, size: size), animated: true)
} else { // zoom out
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
}
}
@ft-z
Copy link

ft-z commented Mar 4, 2025

Thanks! It helps me a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment