I\'m trying to make a pinch to zoom camera but I\'m encountering two problems. First is that it allows the user to zoom way too much in and way to much out, secondly when I take
You can avoid saving prevZoomFactor
by simply resetting UIPinchGestureRecognizer.scale
to 1 like this:
@IBAction func pinchAction(_ sender: UIPinchGestureRecognizer) {
guard let device = currentCaptureDevice else {return}
var zoom = device.videoZoomFactor * sender.scale
sender.scale = 1.0
var error:NSError!
do{
try device.lockForConfiguration()
defer {device.unlockForConfiguration()}
if zoom >= device.minAvailableVideoZoomFactor && zoom <= device.maxAvailableVideoZoomFactor {
device.videoZoomFactor = zoom
}else{
NSLog("Unable to set videoZoom: (max %f, asked %f)", device.activeFormat.videoMaxZoomFactor, zoom);
}
}catch error as NSError{
NSLog("Unable to set videoZoom: %@", error.localizedDescription);
}catch _{
}
}
This was recommended by Apple at a WWDC event I attended way back when gesture recognizers first came out.