简体   繁体   English

如何在UIPanGestureRecognizer中使用velocityView更快地拖动?

[英]How to make dragging faster using velocityView in UIPanGestureRecognizer?

Having a little trouble understanding how to implement a faster dragging speed for the code I'm using below (written by @PaulSoltz) which allows you to drag an object across the screen. 理解如何为我下面使用的代码(由@PaulSoltz编写)实现更快的拖动速度有一点麻烦,它允许您在屏幕上拖动对象。 I realize you have to use the velocityInView method from UIPanGestureRecognizer , and I understand it returns the x velocity vector and y velocity vector. 我意识到你必须使用UIPanGestureRecognizervelocityInView方法,我理解它返回x速度向量和y速度向量。 If velocity = distance over time, then for instance velocityx = (x2 - x1) / time and I'm unsure how to use this formula to get what I need. 如果velocity =距离随时间变化,那么例如velocityx = (x2 - x1) / time ,我不确定如何使用这个公式得到我需要的东西。 Basically I just want to be able to adjust the speed of my movement to make it a little faster. 基本上我只是希望能够调整我的运动速度,使其更快一点。 Maybe I'm over thinking things, but if anyone could help me understand this it would be appreciated. 也许我在思考问题,但如果有人能帮我理解这一点,我们将不胜感激。 Thanks. 谢谢。

- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer {

    UIView *myView = [gestureRecognizer view];

    CGPoint translate = [gestureRecognizer translationInView:[myView superview]];


    if ([gestureRecognizer state] == UIGestureRecognizerStateChanged || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [myView setCenter:CGPointMake(myView.center.x + translate.x, myView.center.y + translate.y)];
    [gestureRecognizer setTranslation:CGPointZero inView:[myView superview]];
    }
}

只需将平移向量的分量乘以某个常数即可。

[myView setCenter:CGPointMake(myView.center.x + translate.x * 2, myView.center.y + translate.y * 2)];

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

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