UIPinchGestureRecognizer。放大手指的位置,而不仅仅是中心[重复]

Posted

技术标签:

【中文标题】UIPinchGestureRecognizer。放大手指的位置,而不仅仅是中心[重复]【英文标题】:UIPinchGestureRecognizer. Make zoom in location of fingers, not only center [duplicate] 【发布时间】:2014-04-23 13:01:25 【问题描述】:

我可以使用UIPinchGestureRecognizer 来放大UICollectionViewCell 的视图,但是你开始做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 的位置呢?

谢谢

【问题讨论】:

【参考方案1】:

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

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;

【讨论】:

以上是关于UIPinchGestureRecognizer。放大手指的位置,而不仅仅是中心[重复]的主要内容,如果未能解决你的问题,请参考以下文章

UIPinchGestureRecognizer 与 UILongTapGestureRecognizer 的使用没有问题

使用 UIPinchGestureRecognizer 调整 UIView 的大小

UIPinchGestureRecognizer。放大手指的位置,而不仅仅是中心[重复]

UIPinchGestureRecognizer 向内捏“慢”

Swift - UIPageViewController中的UIPinchGestureRecognizer

当图像放大太大时,UIPinchGestureRecognizer 表现得很有趣