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

发布于 2024-11-27 14:21:15 字数 841 浏览 2 评论 0原文

在理解如何为我下面使用的代码(由 @PaulSoltz 编写)实现更快的拖动速度时遇到一些困难,该代码允许您在屏幕上拖动对象。我意识到您必须使用 UIPanGestureRecognizer 中的velocityInView 方法,并且我知道它返回 x 速度向量和 y 速度向量。如果速度=随时间变化的距离,那么例如velocityx = (x2 - x1) / time,我不确定如何使用这个公式来获得我需要的东西。基本上我只是想能够调整我的移动速度,使其更快一点。也许我想太多了,但如果有人能帮助我理解这一点,我将不胜感激。谢谢。

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

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. I realize you have to use the velocityInView method from UIPanGestureRecognizer, and I understand it returns the x velocity vector and y velocity vector. 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. 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]];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行至春深 2024-12-04 14:21:15

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

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

Just multiply the components of the translation vector by some constant.

[myView setCenter:CGPointMake(myView.center.x + translate.x * 2, myView.center.y + translate.y * 2)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文