UIPinchGestureRecognizer。缩放手指的位置,而不仅仅是中心 [英] UIPinchGestureRecognizer. Make zoom in location of fingers, not only center

查看:1263
本文介绍了UIPinchGestureRecognizer。缩放手指的位置,而不仅仅是中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 UIPinchGestureRecognizer UICollectionViewCell 的视图中进行缩放,但它没有'无论你开始制作 UIPinch 手势的地方,总是将缩放放在视图的中心。例如,我想在视图的左上角区域进行捏合,并且必须在我触摸屏幕的位置创建缩放。但如果我这样做,缩放就会在视图的中心创建。

I'm able to use UIPinchGestureRecognizer for making zoom in the View of a UICollectionViewCell, but it doesn't matter the place where you start to make the UIPinch gesture, always the zoom goes in the center of the view. For example, I would like to make pinch in the upper-left area of the view, and the zoom have to be created in the position where I touch the screen. But If I do that, the zoom is created in the center of the view.

这是我用来缩放的代码:

This is the code I use for making zoom:

 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的位置?

So how can I select the position of the UIPinchGesture?

谢谢

推荐答案

您只应用比例转换,它始终保持视图中心。除了缩放之外,您还必须根据发件人的 locationInView 执行翻译。您可以使用以下代码替换上述函数:

You are only applying the scale transform which will always keep the view at the center. Apart from scaling, you also have to perform translation based on the sender's locationInView. You can replace the above function with the following code:

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

这篇关于UIPinchGestureRecognizer。缩放手指的位置,而不仅仅是中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆