5

我想放大和缩小图像视图,我不想为此使用 UIScrollView。所以为此我使用了 UIPinchGestureRecognizer ,这是我的代码 -

[recognizer view].transform = CGAffineTransformScale([[recognizer view] transform], [recognizer scale], [recognizer scale]);
recognizer.scale = 1;

这适用于放大和缩小。但问题是我想放大和缩小特定比例,比如在 UIScrollView 中,我们可以设置 maxZoom 和 minZoom。我找不到任何解决方案,关于 UIPinchGestureRecognizer 的每个教程都只描述了相同的代码。

4

4 回答 4

14

CGFloat __scale在处理手势的类的接口中声明 2 个 ivars CGFloat __previousScale。通过覆盖其中一个函数设置__scale为(确保在此处调用超级构造函数)。1.0init

- (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);
    }
}
于 2012-06-09T08:07:24.783 回答
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
        }

    }
于 2015-06-09T09:15:54.180 回答
1

我有类似的情况。我的要求是,如果 imageView 小于最小尺寸或大于某个最大尺寸,则 imageView 将反弹回其最后一次转换。

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;
}

这里 self 是 imageView。

于 2012-06-09T05:46:15.300 回答
1

如果您view.transform在捏合时登录,您可以看到放大和缩小的图像协调。所以,这个解决方案不能像我预期的那样工作。我做了这样的解决方案;

Obj-C 版本

    - (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
        }
    }
于 2015-10-20T08:42:44.167 回答