I am engaged on an iOS app utilizing Swift and wish to add pinch-to-zoom assist for video playback utilizing AVPlayer. My aim is to let customers zoom in on the video utilizing pinch gestures, much like how photographs might be zoomed with UIImageView.
Right here’s what I want to realize:
- Zoom in/out with pinch gestures on a video participant
- Keep playback throughout zoom (no pause or stutter)
- Clean reset to unique scale after pinch ends
- Cover UI controls whereas zooming, and restore them when zoom ends
Up to now, I’ve tried making use of transforms like this:
@objc func handlePinch(_ gesture: UIPinchGestureRecognizer) {
swap gesture.state {
case .modified:
let scale = gesture.scale
playerLayer.setAffineTransform(CGAffineTransform(scaleX: scale, y: scale))
case .ended, .cancelled:
UIView.animate(withDuration: 0.25) {
self.playerLayer.setAffineTransform(.identification)
}
default:
break
}
}
This works to some extent, however feels glitchy — particularly when zooming again. I’m not sure if reworking AVPlayerLayer like that is finest follow, or if there’s a extra strong method (e.g., wrapping it in a view, utilizing CATransform3D, and many others.).