I\'ve playing around with recognizing the touches in an iOS Application, and I have this simple code
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent
I had the same problem and solved it with this simple code in the touches ended:
NSInteger touchCount = 0;
for (UITouch *touch in touches) {
touchCount++;
if([[event allTouches]count] == touchCount){
// do what you have to do here
}
}
// You will get a warning here but don't care about it
I hope this help!
Inside info:
I am using:
- 1 finger UITapGestureRecognizer
- 1 finger UILongPressGestureRecognizer
- 1 finger double tap UITapGestureRecognizer
- 2 fingers tap UITapGestureRecognizer
- 1 UIPinchGestureRecognizer
And also should receive one finger panning. But what i have found out is that the Pinch gesture was not Ending even if the user had risen one finger. Somehow iOS is still calling the Pinch's selector with "Changed" state.
My solution was to use the touchesBegin/Moved/Ended to catch the pan and use the number of active touches as safety check in case the pinch selector would be called.
Other settings i had to use in making the above configuration:
self.multipleTouchEnabled = YES;
[oneFingerTapRecognizer requireGestureRecognizerToFail:oneFingerLongPressRecognizer];
[oneFingerTapRecognizer requireGestureRecognizerToFail:oneFingerDoubleTapRecognizer];
[oneFingerLongPressRecognizer requireGestureRecognizerToFail:oneFingerDoubleTapRecognizer];
[twoFingersTapRecognizer requireGestureRecognizerToFail:twoFingersPinch];
Try overriding the UIView
's hitTest
method:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
return self;
}
It is possible that when you lift fingers they are not recognised as being in the UIView
, sotouchesEnded
is not called.
Try removing any Gesture Recognizers you have on that view. They can interfere with touchesEnded
.
You'd have to set recognizer.cancelsTouchesInView = false
in Swift or recognizer.cancelsTouchesInView = NO;
in Objective-C
You have to override all touch methods if you do not call super's implementation. So you have to also implement the touchesMoved:withEvent:
and touchesCancelled:withEvent:
methods. Implementations can be empty but you have to do it.
touchesBegan:withEvent:
If you override this method without calling super (a common use pattern), you must also override the other methods for handling touch events, if only as stub (empty) implementations.
Based on UIResponder Class Reference