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.