///上一次缩放比例,默认为1
@property (nonatomic, assign) CGFloat lastScale;
///最大缩放比例,默认为2
@property (nonatomic, assign) CGFloat maxScale;
///最小缩放比例,默认为1
@property (nonatomic, assign) CGFloat minScale;
//设置缩放比例
self.lastScale = 1;
self.minScale = 1;
self.maxScale = 2;
- (void)pinchGestureRecognizer:(UIPinchGestureRecognizer *)recognizer {
switch (recognizer.state) {
case UIGestureRecognizerStateBegan://缩放开始
case UIGestureRecognizerStateChanged://缩放改变
{
CGFloat currentScale = [[self.layer valueForKeyPath:@"transform.scale"] floatValue];
CGFloat newScale = recognizer.scale - self.lastScale + 1;
newScale = MIN(newScale, self.maxScale / currentScale);
newScale = MAX(newScale, self.minScale / currentScale);
self.transform = CGAffineTransformScale(self.transform, newScale, newScale);
self.lastScale = recognizer.scale;
}
break;
case UIGestureRecognizerStateEnded://缩放结束
self.lastScale = 1;
break;
default:
break;
}
}