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
If you need a manual zoomTo(2.0) function, you can use this
// Create listener for Pinch to Zoom
let pinchRecognizer = UIPinchGestureRecognizer(target: self, action:#selector(FSCameraView.pinchToZoom(_:)))
pinchRecognizer.delegate = self
self.previewViewContainer.addGestureRecognizer(pinchRecognizer)
// set the zoom to a zoomed in mode from start
setZoom(CGFloat(2.0)
// and the functions
func pinchToZoom(sender:UIPinchGestureRecognizer) {
var vZoomFactor = ((sender as! UIPinchGestureRecognizer).scale)
setZoom(vZoomFactor)
}
func setZoom(zoomFactor:CGFloat) {
var device: AVCaptureDevice = self.device!
var error:NSError!
do{
try device.lockForConfiguration()
defer {device.unlockForConfiguration()}
if (zoomFactor <= device.activeFormat.videoMaxZoomFactor) {
let desiredZoomFactor:CGFloat = zoomFactor + atan2(sender.velocity, 5.0);
device.videoZoomFactor = max(1.0, min(desiredZoomFactor, device.activeFormat.videoMaxZoomFactor));
}
else {
NSLog("Unable to set videoZoom: (max %f, asked %f)", device.activeFormat.videoMaxZoomFactor, zoomFactor);
}
}
catch error as NSError{
NSLog("Unable to set videoZoom: %@", error.localizedDescription);
}
catch _{
}
}