我想放大和缩小 ImageView ,但我不想为此使用UIScrollView。
所以为此,我使用了UIPinchGestureRecognizer,这是我的代码-
[recognizer view].transform = CGAffineTransformScale([[recognizer view] transform], [recognizer scale], [recognizer scale]);
recognizer.scale = 1;
这对于放大和缩小工作正常。
但是问题是我想放大和缩小特定的比例,例如在UIScrollView中,我们可以设置maxZoom和minZoom。我找不到任何解决方案,每个有关UIPinchGestureRecognizer的教程都只描述相同的代码。
最佳答案
在处理手势的类的界面中声明2个ivat CGFloat __scale
和CGFloat __previousScale
。通过覆盖__scale
函数之一将1.0
设置为init
(确保在此处调用 super 构造函数)。
- (void)zoom:(UIPinchGestureRecognizer *)gesture {
NSLog(@"Scale: %f", [gesture scale]);
if ([gesture state] == UIGestureRecognizerStateBegan) {
__previousScale = __scale;
}
CGFloat currentScale = MAX(MIN([gesture scale] * __scale, MAX_SCALE), MIN_SCALE);
CGFloat scaleStep = currentScale / __previousScale;
[self.view setTransform: CGAffineTransformScale(self.view.transform, scaleStep, scaleStep)];
__previousScale = currentScale;
if ([gesture state] == UIGestureRecognizerStateEnded ||
[gesture state] == UIGestureRecognizerStateCancelled ||
[gesture state] == UIGestureRecognizerStateFailed) {
// Gesture can fail (or cancelled?) when the notification and the object is dragged simultaneously
__scale = currentScale;
NSLog(@"Final scale: %f", __scale);
}
}
关于iphone - 如何使用UIPinchGestureRecognizer设置最小和最大缩放比例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10958624/