2

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?

user773578
  • 1,161
  • 2
  • 13
  • 24

2 Answers2

3

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.

  • The issue is the completion block that each recognizer has. One of the completes before the other and I am not sure where else to set the boolean other than in the completion block. Is it even possible? I want the app to know when 2 gestures are being recognized simultaneously so that I can trigger something. – user773578 Jul 19 '11 at 21:06
  • Ok I understand now. What you should do I think is checking the other GR's `state` property in each block. If the other's state equals `UIGestureRecognizerStateRecognized` then you know if the other gesture has been recognized. –  Jul 19 '11 at 21:19
0

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.

Community
  • 1
  • 1
jscs
  • 63,694
  • 13
  • 151
  • 195