user773578
user773578

Reputation: 1161

Execute after UIGestureRecognizer completion

I have 2 GestureRecognizers that when triggered at the same need to trigger an animation.

I have 2 booleans, 1 for each, that get set to yes when the gesture is recognized.

My issue is that I need to be able to do a check in one recognizer to see if the other recognizer has been triggered.

I currently do the following

[self registerRecognizer:swipeRecognizerRight 
       onRecognizedBlock:^(UIGestureRecognizer *recognizer) {
    NSLog(@"pulled to right");
    leftPulled = TRUE;

    if (rightPulled) {
        [self->delegate executeActionString:someAnimation];       
    }

    leftPulled = FALSE;
}];

and the same for the recognizer on the right.

leftPulled and rightPulled are the actual objects, one on the left, one on the right.

My issue is that the one recognizer gets executed before the other so there will never be a situation when both are recognized and trigger the animation.

How can this be solved? Some kind of timer, or is there a way to code the recognizers so that both can be recognized at the same time and then trigger the animation?

Upvotes: 2

Views: 339

Answers (2)

jscs
jscs

Reputation: 64002

Your idea for a timer is probably the right one. You need to come up with a threshold duration, and simply use performSelector:afterDelay:, or GCD and a block* (link to SO question) to reset your flags:

int64_t threshold = 1000000;    // In nanoseconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, threshhold),     
               dispatch_get_main_queue(), 
               ^{ leftPulled = FALSE; });

*Which I believe has more accuracy for very small delays than an NSTimer, though I'm not certain.

Upvotes: 0

user756245
user756245

Reputation:

You will find your way I think, in a UIGestureRecognizerDelegate protocol method :

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)g1
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)g2;

In your case this method should return YES in both cases ( ...:g1 ...:g2 and ...:g2 ...:g1 ), to let the two gestures be recognised at the same time, either beginning with g1, or g2.

Upvotes: 3

Related Questions