I want to have UITapGestureRecognizer
in a view and touchesBegan
in its childview, but the problem is when UITapGestureRecognizer
is recognized touchesBegan
is not called.
Is it doing this is fine? Or should I need to take another approach ?
Edit : Solved. Setting cancelsTouchesInView property of UITapGestureRecognizer
did the trick for me. By default it is false, so touchesBegan
isn't called.
Please find out where are you wrong, This is a step by step guide on how to implement gesture recognizers in your class:
Conform your class to
UIGestureRecognizerDelegate
protocol.Instantiate the gesture recognizer. For example, to instantiate a
UITapGestureRecognizer
, we will do:Here, action is the selector which will handle the gesture. Here, our selector handleTapFrom will look something like:
The argument to the selector is the gesture recognizer. We can use this gesture recognizer to access its properties, for example, we can find the state of the gesture recognizer, like,
UIGestureRecognizerStateBegan
,UIGestureRecognizerStateEnded
, etc.Set the desired properties on the instantiated gesture recognizer. For example, for a
UITapGestureRecognizer
, we can set the propertiesnumberOfTapsRequired
, andnumberOfTouchesRequired
.Add the gesture recognizer to the view you want to detect gestures for. In our sample code (I will be sharing that code for your reference), we will add gesture recognizers to an imageView with the following line of code:
After adding the gesture recognizer to the view, set the delegate for the gesture recognizer, i.e. the class which will handle all the gesture recognizer stuff. In our sample code, it would be like:
Note: Assign the delegate after adding the gesture recognizer to the view. Otherwise, the action method won’t be called.