UIPinchGestureRecognizer. Make zoom in location of fingers, not only center [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 23:04:08

问题


This question already has an answer here:

  • UIPinchGestureRecognizer position the pinched view between the two fingers 2 answers

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

So how can I select the position of the UIPinchGesture?

Thanks


回答1:


I did it, this is my final code:

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


来源:https://stackoverflow.com/questions/23245127/uipinchgesturerecognizer-make-zoom-in-location-of-fingers-not-only-center

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!