如何减慢UIPanGestureRecognizer的速度?

发布于 2024-10-19 21:32:23 字数 463 浏览 2 评论 0原文

我有一个方法,当识别到 2 指平移手势时,我想调用该方法。我已经设置好并且工作正常,但问题是我只需要大约 15 次调用该方法(它过滤图像),当我平移大约一英寸时,该方法已被调用一百遍,图像转得如此之快,我不知道发生了什么。

我该怎么做才能减慢手势识别器的速度?

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:2];
[panRecognizer setMaximumNumberOfTouches:2];
[panRecognizer setDelegate:self];
[self view] addGestureRecognizer:panRecognizer]];

I have a method that I would like to call when a 2 finger pan gesture is recognized. I have it setup and working ok, but the problem is that there is only about 15 times I need the method to be called (it filters through images), and by the time I've panned about an inch, the method has been called a hundred times and the images went by so fast I didn't know what was going on.

What can I do to slow down my gesture recognizer?

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:2];
[panRecognizer setMaximumNumberOfTouches:2];
[panRecognizer setDelegate:self];
[self view] addGestureRecognizer:panRecognizer]];

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

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

发布评论

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

评论(3

满栀 2024-10-26 21:32:23

想必您每次收到平移事件时都会更改图像。这不太好。相反,您应该向平移手势识别器询问拖动距离(使用 -translationInView:),并且仅在超过特定阈值后才更改图像。

Presumably you're changing images every time you get a pan event. That's not very good. Instead you should ask the pan gesture recognizer for the drag distance (use -translationInView:) and only change images once you've passed a specific threshold.

独孤求败 2024-10-26 21:32:23

我创建了一个“responseCount”,基本上捕获每第四个或第五个(有效)手势。

// within method that fires with each gesture:
CGPoint translatedPoint = [(UIPanGestureRecognizer*)panRecognizer translationInView:aView];
if(abs(translatedPoint.x) > 20 || abs(translatedPoint.y) > 20){
    if(responseCount == 4){
        // do animation/response
        responseCount = 0;
    } else {
        responseCount += 1;
    }
}

I created a "responseCount" basically capturing every 4th or 5th (valid) gesture.

// within method that fires with each gesture:
CGPoint translatedPoint = [(UIPanGestureRecognizer*)panRecognizer translationInView:aView];
if(abs(translatedPoint.x) > 20 || abs(translatedPoint.y) > 20){
    if(responseCount == 4){
        // do animation/response
        responseCount = 0;
    } else {
        responseCount += 1;
    }
}
姐不稀罕 2024-10-26 21:32:23

斯威夫特4

@objc func panGestureHandler(_ gesture: UIPanGestureRecognizer) {

    let theViewMinimumY = someValue
    let translation = gesture.translation(in: gesture.view)

    switch gesture.state {

    case .began:
        gesture.setTranslation(CGPoint.zero, in: gesture.view)

    case .changed:

        gesture.setTranslation(CGPoint.zero, in: gesture.view)

        // if the view ever goes beyond a certain point
        if theView.frame.origin.y < theViewMinimumY {

            // only add a fraction of the gesture's translation (in this case 50%)
            theView.center = CGPoint(x: theView.center.x, y: theView.center.y + (translation.y * 0.5))

        } else {

            theView.center = CGPoint(x: theView.center.x, y: theView.center.y + translation.y)

        }

    case .ended:

        ...

    default:
        break

    }

}

Swift 4

@objc func panGestureHandler(_ gesture: UIPanGestureRecognizer) {

    let theViewMinimumY = someValue
    let translation = gesture.translation(in: gesture.view)

    switch gesture.state {

    case .began:
        gesture.setTranslation(CGPoint.zero, in: gesture.view)

    case .changed:

        gesture.setTranslation(CGPoint.zero, in: gesture.view)

        // if the view ever goes beyond a certain point
        if theView.frame.origin.y < theViewMinimumY {

            // only add a fraction of the gesture's translation (in this case 50%)
            theView.center = CGPoint(x: theView.center.x, y: theView.center.y + (translation.y * 0.5))

        } else {

            theView.center = CGPoint(x: theView.center.x, y: theView.center.y + translation.y)

        }

    case .ended:

        ...

    default:
        break

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文