简体   繁体   English

iOS:使用带惯性和头顶的Pinch手势进行缩放

[英]iOS: Zoom using Pinch gesture with inertia and overhead

I am fairly new to iOS and I am trying to figure out how to use a Pinch gesture to zoom with inertia and overhead (I do not know if the word overhead is correct in this context, in german it would be called "Überschwingen"). 我是iOS的新手,我想弄清楚如何使用捏捏手势来进行惯性和头顶缩放(我不知道头顶头这个单词在这种情况下是否正确,在德语中会称为“Überschwingen”) 。

Basically what it shall do: It should have a max and min scale (in my case 1.0 to 4.0) in which you can zoom. 基本上应该做什么:它应该具有可以缩放的最大和最小比例(在我的情况下为1.0到4.0)。 When the gesture is finished it should take the given velocity and make a curve out animation, also allowing the view to over- and underflow the given scales and then move back to the min or max like with tension. 手势完成后,它应该以给定的速度运动并制作出曲线动画,还允许视图以给定的比例上下浮动,然后像拉力一样移回最小值或最大值。

I got the Gesture Recognizer running for this and also managed to get it make use of my minimum and maximum scale (using examples from stackoverflow). 为此,我运行了Gesture Recognizer,并且设法利用我的最小和最大比例(使用stackoverflow的示例)。 This is what I got so far: 这是我到目前为止所得到的:

- (void)handle_pinch:(UIPinchGestureRecognizer *)recognizer
{

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

    if ([recognizer state] == UIGestureRecognizerStateChanged) {

        CGFloat currentScale = [[[recognizer 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 - [recognizer scale]); // new scale is in the range (0-1)
        newScale = MIN(newScale, kMaxScale / currentScale);
        newScale = MAX(newScale, kMinScale / currentScale);
        scale = newScale;

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

        [recognizer view].transform = transform;

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

        [recognizer view].transform = transformTranslate;
        NSLog(@"Transformed");
    }       
}

But I do now know how I can add the animation here. 但是我现在知道如何在此处添加动画。 Thanks for any help! 谢谢你的帮助!

You should be using a UIScrollView to achieve the pinch to zoom effect as UIScrollView is already integrated along with animation. 您应该使用UIScrollView来实现缩放效果,因为UIScrollView已经与动画集成在一起。 Just add your UIView inside a UIScrollView. 只需将您的UIView添加到UIScrollView中即可。

Here is great tutorial on UIScrollView. 这是关于UIScrollView的出色教程。 He is using a UIImageView but UIView will behave in a similar way. 他正在使用UIImageView,但是UIView的行为类似。

https://www.raywenderlich.com/122139/uiscrollview-tutorial https://www.raywenderlich.com/122139/uiscrollview-tutorial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM