UIPinchGestureRecognizer。缩放手指的位置,而不仅仅是中心

时间:2014-04-23 13:01:26

标签: ios uigesturerecognizer uipinchgesturerecognizer

我可以使用UIPinchGestureRecognizerUICollectionViewCell的视图中进行缩放,但是您开始制作UIPinch的地方并不重要手势,始终缩放位于视图的中心。例如,我想在视图的左上角区域进行捏合,并且必须在我触摸屏幕的位置创建缩放。但是,如果我这样做,则会在视图的中心创建缩放。

这是我用来制作缩放的代码:

 if([gesture state] == UIGestureRecognizerStateBegan) {
        previousScale = 1.0;

    }

    if (
        [gesture state] == UIGestureRecognizerStateChanged) {

        CGFloat currentScale = [[[gesture view].layer valueForKeyPath:@"transform.scale"] floatValue];

        // Constants to adjust the max/min values of zoom
        const CGFloat kMaxScale = 4.0;
        const CGFloat kMinScale = 1.0;

        CGFloat newScale = 1 -  (previousScale - [gesture scale]); // new scale is in the range (0-1)
        newScale = MIN(newScale, kMaxScale / currentScale);
        newScale = MAX(newScale, kMinScale / currentScale);
        scale = newScale;

        CGAffineTransform transform = CGAffineTransformScale([[gesture view] transform], newScale, newScale);

        [gesture view].transform = transform;

        [self.collectionView.collectionViewLayout invalidateLayout];
    }

那么如何选择UIPinchGesture的位置?

由于

2 个答案:

答案 0 :(得分:18)

您只应用缩放变换,它始终将视图保持在中心位置。除了缩放之外,您还必须根据发件人locationInView执行翻译。您可以使用以下代码替换上述功能:

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender {


    if (sender.state == UIGestureRecognizerStateBegan) {
        lastScale = 1.0;
        lastPoint = [sender locationInView:self];
    }

    // Scale
    CGFloat scale = 1.0 - (lastScale - sender.scale);
    [self.layer setAffineTransform:
        CGAffineTransformScale([self.layer affineTransform], 
                               scale, 
                               scale)];
    lastScale = sender.scale;

    // Translate
    CGPoint point = [sender locationInView:self];
    [self.layer setAffineTransform:
        CGAffineTransformTranslate([self.layer affineTransform], 
                                   point.x - lastPoint.x, 
                                   point.y - lastPoint.y)];
    lastPoint = [sender locationInView:self];
}

答案 1 :(得分:4)

我做到了,这是我的最终代码:

if([gesture state] == UIGestureRecognizerStateBegan) {
    previousScale = 1.0;
    lastPoint = [gesture locationInView:[gesture view]];
}

if (
    [gesture state] == UIGestureRecognizerStateChanged) {

    CGFloat currentScale = [[[gesture view].layer valueForKeyPath:@"transform.scale"] floatValue];

    // Constants to adjust the max/min values of zoom
    const CGFloat kMaxScale = 4.0;
    const CGFloat kMinScale = 1.0;

    CGFloat newScale = 1 -  (previousScale - [gesture scale]); // new scale is in the range (0-1)
    newScale = MIN(newScale, kMaxScale / currentScale);
    newScale = MAX(newScale, kMinScale / currentScale);
    scale = newScale;

    CGAffineTransform transform = CGAffineTransformScale([[gesture view] transform], newScale, newScale);

    [gesture view].transform = transform;

    CGPoint point = [gesture locationInView:[gesture view]];
    CGAffineTransform transformTranslate = CGAffineTransformTranslate([[gesture view] transform], point.x-lastPoint.x, point.y-lastPoint.y);

    [gesture view].transform = transformTranslate;
}