如何降低UIPanGestureRecognizer的速度?

时间:2011-03-02 22:03:37

标签: iphone methods performance uigesturerecognizer

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

我可以做些什么来减慢手势识别器的速度?

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

3 个答案:

答案 0 :(得分:2)

据推测,每次收到平移事件时,您都会更改图像。那不是很好。相反,您应该向平移手势识别器询问拖动距离(使用-translationInView:),并且只有在超过特定阈值时才更改图像。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

    }

}