Listening to a UITouch Event with UIGestureRecognizer

I create a custom UIView and add a UITapGestureRecognizer to it. I have a tap gesture handler. But at the same time, I want my UIView to listen to Began's touches and touch the Ended methods. I implemented the gestureRecognizer: shouldReceiveTouch: method as well, but the Began / touchesEnded methods are not called. Any clue why?

Inside my custom UIView

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)iGestureRecognizer shouldReceiveTouch:(UITouch *)iTouch { return YES; } 

Inside my view controller

 MyCustomView aCustomView = [[[MyCustomView alloc] init] autorelease]; UIGestureRecognizer *myGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [aCustomView addGestureRecognizer:myGestureRecognizer]; [myGestureRecognizer release]; 
+7
source share
2 answers

You need to set cancelsTouchesInView (and most likely delaysTouchesBegan and delaysTouchesEnded ) to NO for the gesture recognizer. The default gesture recognizer behavior is to avoid touching it and the viewing process. These settings let you fine tune this behavior.

+10
source

As stated earlier, you need to set the cancelTouchesInView property to NO on the UITapGestureRecognizer .

From Apple docs:

cancels touchesInView. If the gesture recognizer recognizes its gesture, it repels the remaining strokes of this gesture from their point of view (so the window will not deliver them). The window cancels previously delivered touches using the message (touchsCancelled: withEvent :). If the gesture recognizer does not recognize its gesture, the view gets an all-touching sequence with a few touches.

Further reading: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/

0
source

All Articles