Domanda

So my current project has a pan gesture recognizer, and if I have panned to the top of the screen the screen it is supposed to scroll up to account for that gesture. While the gesture hasn't ended and the current position of the gesture remains at the top of the screen, I want to continually keep scrolling. My problem is the gesture recognizer only gets called when the state changes, therefore my content will only scroll if you move back and forth at the top, not continually while the gesture continues to be at the top. Is there any reasonable way to continually call code while the gesture hasn't ended, but isn't necessarily changing? Here is pseudo-code of what I have:

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

I can think of a few ghetto ways to do it by setting state bools based on the current state of the recognizer and creating my own timer to periodically check, but it seems pretty hacky and I don't particularly like it, so I'm wondering if anyone could come up with a cleaner solution.

È stato utile?

Soluzione

One way to use a timer and feel better about it would be to conceal it a little by using 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];
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top