Pan手势尚未结束的定期事件

时间:2014-03-26 20:19:38

标签: ios objective-c uigesturerecognizer uipangesturerecognizer

所以我当前的项目有一个平移手势识别器,如果我已经平移到屏幕顶部的屏幕,它应该向上滚动以说明该手势。虽然手势没有结束并且手势的当前位置保持在屏幕的顶部,但我想继续滚动。我的问题是手势识别器仅在状态改变时才被调用,因此我的内容只会在您在顶部来回移动时滚动,而不是在手势继续在顶部时连续移动。有没有合理的方法来连续调用代码而手势还没有结束,但是不一定要改变?这是我的伪代码:

- (void)handleGestureRecognizer:(UIGestureRecognizer *)gesture {
    if ( gesture.state == UIGestureRecognizerStateChanged ) {
        CGPoint point = [gesture locationInView:self.view];
        if (point.y < 100) {
            //I would like this code to be called continually while the
            //gesture hasn't ended, not necessarily only when it changes
            [self updateScrollPosition];
        }
}

我可以通过根据识别器的当前状态设置状态bool并创建我自己的计时器来定期检查,以此来考虑一些贫民窟的方法,但它看起来非常h​​acky并且我不是特别喜欢它,所以我想知道是否有人能提出更清洁的解决方案。

1 个答案:

答案 0 :(得分:1)

一种使用计时器并且感觉更好的方法是使用performSelector隐藏它:withObject:afterDelay:

- (void)stillGesturing {
    [self updateScrollPosition];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [self performSelector:@selector(stillGesturing) withObject:nil afterDelay:0.5];
}

// then in the recognizer target
if (gesture.state == UIGestureRecognizerStateEnded) {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
} else if ( gesture.state == UIGestureRecognizerStateChanged ) {
    CGPoint point = [gesture locationInView:self.view];
    if (point.y < 100) {
        //I would like this code to be called continually while the
        //gesture hasn't ended, not necessarily only when it changes
        [self stillGesturing];
    }
}