I have a problem, I have a UIPinchGestureRecognizer that I'm using to scale my camera preview and respective AVCaptureConnection in and out. There is no problem in zooming in and out the first time until you lift your fingers after you're done pinching to zoom. If I try to pinch inwards (zooming back to normal 1.0 scale), the gesture doesn't reset the preview and AVCaptureConnection scales lower, and therefore the preview and AVCaptureConnection stay at the same size.
How could I go about fixing this? The if statement makes sure that the scale doesn't go below 1.0, as we're not allowed to set a scale to below 1.0 for the AVCaptureConnection.
- (void)zoomPreview:(UIPinchGestureRecognizer *)recognizer
{
CGFloat scale = recognizer.scale;
NSLog(@"Scale: %f",scale);
if (scale >= 1.0)
{
[[self.stillImageOutput.connections objectAtIndex:0] setVideoScaleAndCropFactor:scale];
self.previewView.transform = CGAffineTransformMakeScale(scale, scale);
}
}
Solved myself through trial and error:
@property (nonatomic, assign) CGFloat lastScale;
@property (nonatomic, assign) CGFloat currentScale;
- (void)zoomPreview:(UIPinchGestureRecognizer *)recognizer
{
self.currentScale += recognizer.scale - self.lastScale;
self.lastScale = recognizer.scale;
if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled)
{
self.lastScale = 1.0;
}
if (self.currentScale < 1.0)
{
self.currentScale = 1.0;
}
if (self.currentScale > 5.0)
{
self.currentScale = 5.0;
}
if (self.currentScale >= 1.0 && self.currentScale <= 5.0)
{
[[self.stillImageOutput.connections objectAtIndex:0]setVideoScaleAndCropFactor:self.currentScale];
self.previewView.transform = CGAffineTransformMakeScale(self.currentScale, self.currentScale);
}
}