I. Gesture recognizers
Gesture recognizers is an object introduced in iOS3.2 that can be used to identify gestures and simplify custom view event handling. The base class of Gesture recognizers is Uigesturerecognizer, an abstract base class that defines the programming interface for implementing the underlying gesture recognition behavior. 6 specific Gesture recognition classes are provided in the Uikit framework to identify common gestures. These 6 gesture recognizer classes are:
UITapGestureRecognizer: Used to identify click gestures, including clicks, double clicks, and even three clicks.
Uipinchgesturerecognizer: Used to identify finger pinch gestures.
Uipangesturerecognizer: Used to identify the drag gesture.
Uiswipegesturerecognizer: Used to identify swipe gestures.
Uirotationgesturerecognizer: Used to identify rotation gestures.
Uilongpressgesturerecognizer: Used to identify long press gestures.
To identify gestures, you need to associate the gesture recognizers with the view on which it detects touch events, and you can use the UIView Addgesturerecognizer: method to bind the gesture recognizer to the view. Gesture recognizers is in the role of the observer in the Touch event processing process, which is not part of the view hierarchy and therefore does not participate in the responder chain. Before you send a touch event to Hit-test view, the system will first send a touch event to gesture recognizers bound on Hit-test view or hit-test View Parent view (Superview). The process is probably as follows:
Note: The relationship between View and Gesture recognizer in the figure is that Gesture recognizer is associated with the Superview (possibly multilevel) of the view or view.
Two. Relationship between Gesture recognizers and the event distribution path
Gesture Recognizers may delay the sending of touch events to Hit-test view, by default, when the gesture is recognized by Gesture recognizers, the cancel message is sent to the Hit-test view. To cancel an event that was previously sent to Hit-test view. Control of this process is the three properties of Uigesturerecognizer
Cancelstouchesinview (default = yes)
Delaystouchesbegan (default is NO)
delaystouchesended (default = yes)
Cancelstouchesinview is yes, indicating that when gesture Recognizers recognizes the gesture, it sends Touchescancelled:withevent: message to Hit-test view to cancel Hit-test View handles this touch sequence so that only gesture recognizers can respond to this touch sequence, hit-test view no longer responds. If no, the Touchescancelled:withevent: message is sent to Hit-test view, which causes gesture recognizers and hit-test view to respond to the touch sequence simultaneously.
Delaystouchesbegan is no, which means that when the touch sequence starts, and the gesture recognizer does not recognize the gesture, the touch event is sent to Hit-test view at the same time, so that the gesture is not recognized by the gesture recognizer Hit-test View can also receive the same touch event. If yes, no touch events are sent to Hit-test view while the gesture recognizer is identifying the gesture, and no message is delivered if the gesture recognizer eventually recognizes the gesture (including Touchescancelled:withevent:) to Hit-test view; some gesture recognition eventually does not recognize the gesture, then all touch events are sent to Hit-test view processing. For this feature, refer to the Delayscontenttouches property of Uiscrollview. This property is also used sparingly, and improper use can cause the UI to become unresponsive.
Delaystouchesended, the explanation on the document is that when the gesture recognizer recognizes gestures, the touch of the uitouchphaseended phase is delayed to Hit-test view, and after the gesture recognition is successful, it is sent to Hit-test The view cancel message, when gesture recognition fails, sends the original end message. It gives an example of the UITapGestureRecognizer object that identifies the double-click operation, whose numberoftapsrequired is set to 2, and if delaystouchesended is no when the user makes a double-click operation, The call order in Hit-test view is listed as
Touchesbegan:withevent:,
Touchesended:withevent:,
Touchesbegan:withevent:,
and touchescancelled:withevent:
If delaystouchesended is yes, the call sequence is:
Touchesbegan:withevent:,
Touchesbegan:withevent:,
Touchescancelled:withevent:,
Touchescancelled:withevent:
But when I was actually testing, that was not the case, the result of the actual test is that if delaystouchesended is no, then the call sequence is:
Touchesbegan:withevent:,
Touchesended:withevent:,
Tapgesturerecognizer detected a double-click
If delaystouchesended is yes, the call sequence is:
Touchesbegan:withevent:,
Touchesended:withevent:,
Tapgesturerecognizer detected a double-click
Touchescancelled:withevent:
The problem is not clear!
Three. Relationships between multiple gesture recognizer
Multiple gesture recognizer can be bound on a view, and by default touch events in the touch sequence are passed in an indeterminate order across gesture recognizer until the event is eventually sent to Hit-test View (if the middle is not identified and intercepted by gesture recognizer). The relationships between multiple gesture recognizer can also be tailored to your needs, mainly in the following types of behaviors
1. In case one of the gesture recognizer fails, the other gesture recognizer can parse the event.
To identify both the click action and the double-click operation as an example, two gesture recognizers are used to identify the click and double clicks, respectively, Singletapgesture and Doubletapgesture. By default, when a user clicks, Singletapgesture recognizes a single click, and Doubletapgesture also recognizes a double-click action, but our intention is that this is just a double-click operation. In this case we can use Uigesturerecognizer's requiregesturerecognizertofail: method to enable Singletapgesture to parse an event when doubletapgesture recognition is recognized, and if Doubletapgesture identifies a double-click event, Singletapgesture will not have any action.
[Singletapgesture Requiregesturerecognizertofail:doubletapgesture];
It is important to note that, in this case, if a user clicks a delay (that is, the doubletapgesture recognition fails), Singletapgesture will recognize the click action and click Processing, which is a lot of time and has little effect on actual use.
2. Precisely control whether the gesture recognizer responds to an event or sequence of events.
There are two optional methods in the Uigesturerecognizerdelegate protocol to control whether gesture recognizer need to identify certain events
Gesturerecognizershouldbegin:
This method is called when the gesture recognizer view goes out of the uigesturerecognizerstatepossible state, and if no is returned, it is converted to uigesturerecognizerstatefailed; If yes is returned, the touch sequence continues to be recognized. (Yes by default)
Gesturerecognizer:shouldreceivetouch:
This method is called before the Window object calls the Touchesbegan:withevent: method of the gesture recognizer when a touch event occurs, and if no is returned, gesture recognizer will not see this touch event. (yes by default).
In addition, there are two methods that can be overridden in the Uigesturerecognizer class to accomplish the same functionality as in the delegate method
-(BOOL) Canpreventgesturerecognizer: (Uigesturerecognizer *) Preventedgesturerecognizer;
-(BOOL) Canbepreventedbygesturerecognizer: (Uigesturerecognizer *) Preventinggesturerecognizer;
3. Allow multiple gesture recognizers to identify together
By default, two gesture recognizers do not recognize their gestures at the same time, but you can implement the Uigesturerecognizerdelegate protocol
Gesturerecognizer:shouldrecognizesimultaneouslywithgesturerecognizer: Method to control it. This method is called when any one of these two gesture recognizers will block another touch event, and if yes is returned, two gesture recognizers can be identified simultaneously, and if no is returned, two gesture is not guaranteed Recognizers must not be recognized at the same time, because this method of another gesture recognizer may return yes. That is to say, the delegate method of the two gesture recognizers only returns Yes if any one, then the two can be identified at the same time, only two return no, is mutually exclusive. By default, no is returned.
For example, if you want to detect all touch events on a window, you can associate the gesture recognizer to the window, by default if the gesture is recognized by the window, the gesture recognizer in the child view is invalidated. The purpose of our gesture recognizer on window is simply to monitor all events, but not to deal with them, and the handling of the events requires each gesture recognizer in the sub-view to be processed, This allows us to implement the delegate method of binding gesture recognizer to the window, so that the Gesturerecognizer: Shouldrecognizesimultaneouslywithgesturerecognizer: Returns YES.
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer {
return YES;
}
Four. Similar behavior of Uiscrollview
Scroll view does not have a scroll bar, and when there is touch behavior on scroll view, it recognizes that the purpose of the touch behavior is to scroll view itself or its content sub-view. Customizing ScrollView How to handle this situation, see the following properties and methods of the Uiscrollview class.
–touchesshouldbegin:withevent:incontentview:
–touchesshouldcancelincontentview:
Cancancelcontenttouches
Delayscontenttouches
Reference:
Event Handling Guide for Ios–gesture recognizers
Uigesturerecognizer Class Reference
Uigesturerecognizerdelegate Protocol Reference
Detecting all touches in an app
Uiscrollview Class Reference
How to recognize swipe gesture in Uiscrollview
Uigesturerecognizer blocks Subview for handling touch events
UIButton Touch is delayed if in Uiscrollview
Why are scrolling a uitableview much more responsive than scrolling a uiscrollview?
How to cancel touches exactly like Uiscrollview?
Original link: http://blog.csdn.net/chun799/article/details/8194893
IOS parsing gesture Recognition (Gesture recognizers)