ios - How to implement Pinch To Zoom in camera (Swift 3)? -
i want implement simple pinch zoom gesture in qr app.
seems simple, did researches found few possible answers problem:
avcapturedevice camera zoom
how implement "pinch zoom" in custom camera
pinch zoom camera
zooming while capturing video using avcapture in ios
unfortunately none of solve wanted, liked first 1 (in objective-c) , tried in swift 3.
i posted answer code came after tries, other easier/better/simpler solutions welcomed :)
i used pinch gesture recognizer
storyboard, linked action:
@ibaction func pinchtozoom(_ sender: uipinchgesturerecognizer) { guard let device = capturedevice else { return } if sender.state == .changed { let maxzoomfactor = device.activeformat.videomaxzoomfactor let pinchvelocitydividerfactor: cgfloat = 5.0 { try device.lockforconfiguration() defer { device.unlockforconfiguration() } let desiredzoomfactor = device.videozoomfactor + atan2(sender.velocity, pinchvelocitydividerfactor) device.videozoomfactor = max(1.0, min(desiredzoomfactor, maxzoomfactor)) } catch { print(error) } } }
note capturedevice
optional object of class avcapturedevice
.
Comments
Post a Comment