我想放大并缩小图像视图,我不想使用UIScrollView。 所以为此我使用了UIPinchGestureRecognizer,这是我的代码 -
[recognizer view].transform = CGAffineTransformScale([[recognizer view] transform], [recognizer scale], [recognizer scale]);
recognizer.scale = 1;
这适用于放大和缩小。 但问题是我想放大和缩小特定比例,如在UIScrollView中我们可以设置maxZoom和minZoom。我找不到任何解决方案,每个关于UIPinchGestureRecognizer的教程都只描述相同的代码。
答案 0 :(得分:14)
在处理手势的类的界面中声明2个ivars CGFloat __scale
和CGFloat __previousScale
。通过覆盖其中一个__scale
函数将1.0
设置为init
(确保在此处调用超级构造函数)。
-(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);
}
}
答案 1 :(得分:2)
– (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer
{
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [gestureRecognizer scale];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@”transform.scale”] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 – (lastScale – [gestureRecognizer scale]); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;
lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
答案 2 :(得分:1)
if ((self.frame.size.width > IMAGE_MIN_SIZE) && (self.frame.size.height > IMAGE_MIN_SIZE) && (self.frame.size.width < IMAGE_MAX_SIZE) && (self.frame.size.height < IMAGE_MAX_SIZE)) {
lastSizeTransform = self.transform;
}else {
self.transform = lastSizeTransform;
}
这里的自我是imageView。
答案 3 :(得分:1)
如果您在捏合时记录了view.transform
,则可以看到放大和缩小的图像协调。所以,这个解决方案并不像我预期的那样有效。我这样做了我的解决方案;
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer {
[recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform,
recognizer.scale, recognizer.scale)];
if (recognizer.view.transform.a > 1.6) {
CGAffineTransform fooTransform = recognizer.view.transform;
fooTransform.a = 1.6; // this is x coordinate
fooTransform.d = 1.6; // this is y coordinate
recognizer.view.transform = fooTransform;
}
if (recognizer.view.transform.a < 0.95) {
CGAffineTransform fooTransform = recognizer.view.transform;
fooTransform.a = 0.95; // this is x coordinate
fooTransform.d = 0.95; // this is y coordinate
recognizer.view.transform = fooTransform;
}
recognizer.scale = 1.0;
}
func handlePinchGesture(recognizer: UIPinchGestureRecognizer) {
if let view = recognizer.view {
view.transform = CGAffineTransformScale(view.transform,
recognizer.scale, recognizer.scale)
if CGFloat(view.transform.a) > 1.6 {
view.transform.a = 1.6 // this is x coordinate
view.transform.d = 1.6 // this is x coordinate
}
if CGFloat(view.transform.d) < 0.95 {
view.transform.a = 0.95 // this is x coordinate
view.transform.d = 0.95 // this is x coordinate
}
recognizer.scale = 1
}
}