I’m working on an iOS app using Swift and would like to add pinch-to-zoom support for video playback using AVPlayer. My goal is to let users zoom in on the video using pinch gestures, similar to how images can be zoomed with UIImageView.
Here’s what I need to achieve:
- Zoom in/out with pinch gestures on a video player
- Maintain playback during zoom (no pause or stutter)
- Smooth reset to original scale after pinch ends
- Hide UI controls while zooming, and restore them when zoom ends
So far, I’ve tried applying transforms like this:
@objc func handlePinch(_ gesture: UIPinchGestureRecognizer) {
switch gesture.state {
case .changed:
let scale = gesture.scale
playerLayer.setAffineTransform(CGAffineTransform(scaleX: scale, y: scale))
case .ended, .cancelled:
UIView.animate(withDuration: 0.25) {
self.playerLayer.setAffineTransform(.identity)
}
default:
break
}
}
This works to some extent, but feels glitchy — especially when zooming back. I’m unsure if transforming AVPlayerLayer like this is best practice, or if there’s a more robust approach (e.g., wrapping it in a view, using CATransform3D, etc.).