iphone - long tap firing two times the same method
I have a UIView custom class and the object created by this class has a long gesture recognizer, added as this
UILongPressGestureRecognizer *tapAndHold =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(doStuff:)];
[tapAndHold setDelegate:self];
[tapAndHold setCancelsTouchesInView:NO];
[tapAndHold setDelaysTouchesEnded:NO];
[self addGestureRecognizer:tapAndHold];
[tapAndHold release];
the problem is that for 开发者_如何学编程each long tap on the object the doStuff method runs two times in sequence.
am I missing something? ThANKS
The gesture recognizer propably fires more than once because there is more than one relevant event about which your delegate needs to be informed. For example, it could fire once it suspects a long tap is in progress, and a second time when an actual long tap ended.
The gesture recognizer will send a reference to itself which you can use to query it about the nature of the event that led to the selector being called. Basically you'd check your gesture recognizers state
property and decide upon its value whether your methods code should be executed or not.
update: your doStuff could look like this:
-(void)doStuff: (UIGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
//do the actual stuff
}
}
精彩评论