You will need to add the zoomFactor variable to your camera.
var zoomFactor: CGFloat = 1.0
Next, determine the scaling of the function to be used with the recognition pinch. I assume that you have created a front capture and input device. frontDevice is an additional capture device on my camera. This is how I scale this device.
public func zoom(pinch: UIPinchGestureRecognizer) { guard let device = frontDevice else { return } func minMaxZoom(_ factor: CGFloat) -> CGFloat { return min(max(factor, 1.0), device.activeFormat.videoMaxZoomFactor) } func update(scale factor: CGFloat) { do { try device.lockForConfiguration() defer { device.unlockForConfiguration() } device.videoZoomFactor = factor } catch { debugPrint(error) } } let newScaleFactor = minMaxZoom(pinch.scale * zoomFactor) switch pinch.state { case .began: fallthrough case .changed: update(scale: newScaleFactor) case .ended: zoomFactor = minMaxZoom(newScaleFactor) update(scale: zoomFactor) default: break } }
Finally, add a pinch recognizer to some view.
let pgr = UIPinchGestureRecognizer(target: self, action: #selector(zoom)) view.addGestureRecognizer(pgr)
source share