0

I am currently making a game in SpriteKit and am using UILongPressGesture since I only want to recognize one touch at a time and want to handle both taps and swipes. The game plays fine but if the app is left or the device is put to sleep (more consistently, this is when the app is put to sleep), upon returning to the game the action I specified for it in initWithTarget:action: is not being called at all if a finger slides around or a quick swipe is made.

If I tap, it gets called. If I tap, hold a little and then begin to slide my finger around it gets called intermittently instead of constantly as it was upon a fresh app start. The other problem is that it does not always happen and it (seemingly) just fixes itself.

Code:

// The recognizer
_tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(recognizeTheTap:)];
[_tapRecognizer setNumberOfTapsRequired:0];
[_tapRecognizer setNumberOfTouchesRequired:1];
[_tapRecognizer setMinimumPressDuration:0];
_tapRecognizer.allowableMovement = 0;
[_tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:_tapRecognizer];

// The action
-(void)recognizeTheTap:(UIGestureRecognizer*)recognizer{
    NSLog(@"Recognizing The Tap");
    CGPoint thePoint = [recognizer locationInView:recognizer.view];
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        NSLog(@"Tap began");
        [self handleTapBegan:thePoint];
    } else if (recognizer.state == UIGestureRecognizerStateChanged){
        NSLog(@"Tap changing?");
        [self handleTapMoved:thePoint];
    } else if (recognizer.state == UIGestureRecognizerStateEnded){
        NSLog(@"Tap ended.");
        [self handleTapEnded:thePoint];
    } else if (recognizer.state == UIGestureRecognizerStateCancelled){
        NSLog(@"Tap canceled");
        [self handleTapEnded:thePoint];
    } else if (recognizer.state == UIGestureRecognizerStateFailed){
        NSLog(@"Tap failed");
    } else {
        NSLog(@"WHAT IS THIS?\n%@", recognizer);
    }
}

Digging around a bit more it seems that when this is going on gestureRecognizer: shouldReceiveTouch: still gets called but gestureRecognizerShouldBegin: does not.

Possible "answer"?

iAd popped up and I tapped it and it did not load for most of the taps. Eventually it did. After commenting out the adding of the UILongPressGestureRecognizer iAd had no problem loading. I implemented touchesBegan:withEvent: and the others and had them call my handleTapWhatever functions and have not yet been able to get the bug to happen again.

So I assume it has something to do with this but as handling it with touchesBegan:withEvent: is fine for me, that seems like the safest option for now.

Community
  • 1
  • 1
revenantkioku
  • 111
  • 1
  • 8
  • Can you post the code which handles the long press gesture (selector) as well as it's declaration? – ZeMoon May 26 '14 at 04:29
  • Added. The first `NSLog` does not happen when this bug is going on. – revenantkioku May 26 '14 at 06:56
  • Well, all I can suggest now is for you to disable the gesture recognizer in -applicationWillResignActive and re-enable or re-initialise it in -applicationWillEnterForeground. – ZeMoon May 26 '14 at 07:22
  • As it happens while the game is playing as well, that seems like it should not do anything. Putting the phone to sleep is the easiest way for me to get the bug to appear, but not the only way this occurs. – revenantkioku May 26 '14 at 07:31
  • Then you should look for an alternative to UILongPressGestureRecognizer. You can implement something of your own using the touch delegates. – ZeMoon May 26 '14 at 10:05

0 Answers0