Gesture recognizers and touch event Distribution

Source: Internet
Author: User
ArticleDirectory
    • 1. gesture recognizers

From http://blog.csdn.net/chun799/article/details/8194893

 

1. gesture recognizers

Gesture recognizers is introduced in ios3.2 and can be used to identify gestures and simplify the processing of custom view events. The base class of gesture recognizers is uigesturerecognizer. This abstract base class defines programming interfaces for implementing underlying Gesture Recognition Behavior. Six specific Gesture Recognition classes are provided in the uikit framework to recognize common gestures. The six gesture identifiers are:

    • Uitapgesturerecognizer: Used to recognize click gestures, including clicking, double-clicking, or even three-clicking.
    • Uipinchgesturerecognizer: Used to recognize finger kneading gestures.
    • Uipangesturerecognizer: Used to recognize drag gestures.
    • Uiswipegesturerecognizer: Used to recognize swipe gestures.
    • Uirotationgesturerecognizer: Used to recognize rotation gestures.
    • Uilongpressgesturerecognizer: Used to recognize long-pressed gestures.

To identify a gesture, you need to associate gesture recognizers with the view that detects the touch event. You can use addgesturerecognizer of the uiview to bind the gesture reader to the view. Gesture recognizers is an observer in the touch event processing process. It is not part of the view hierarchy, so it is not involved in the responder chain. Before sending a touch event to the hit-test view, the system sends the touch event to the parent view bound to the hit-test view or hit-test view) on the bound gesture recognizers. The process is as follows:

Note: In the figure, the relationship between view and gesture recognizer is that gesture recognizer is associated with view or view superview (possibly multi-level.

2. Relationship between gesture recognizers and event distribution path

Gesture recognizers may delay sending the touch event to the hit-test view. By default, when gesture recognizers recognizes the gesture, it will send the Cancel message to the hit-test view, to cancel the event sent to hit-test view. This process is controlled by three attributes of uigesturerecognizer

    • Cancelstouchesinview (yes by default)
    • Delaystouchesbegan (No by default)
    • Delaystouchesended (yes by default)
    • The value of cancelstouchesinview is yes, indicating that when gesture recognizers recognizes the gesture, it sends touchescancelled: withevent: Message to hit-test view to cancel processing of the touch sequence, in this way, only gesture recognizers can respond to this touch sequence, and hit-test view no longer responds. If no, do not send the touchescancelled: withevent: Message to hit-test view. This will make gesture recognizers and hit-test view respond to the touch sequence at the same time.

    • When delaystouchesbegan is no, it indicates that when the touch sequence starts and the gesture reader does not recognize this gesture, the touch event will send the hit-test view at the same time, so that the gesture reader does not recognize this gesture, hit-test view can also receive the same touch event. If yes, no touch events are sent to the hit-test view during the gesture recognition process. If yes, then, no messages (including touchescancelled: withevent :) are sent to hit-test view. If some gesture recognition does not recognize the gesture, all the touch events are sent to hit-test view for processing. For details about this feature, refer to the delayscontenttouches attribute of uiscrollview. In this way, attributes are also used with caution. improper use may cause the UI to be unresponsive.

    • Delaystouchesended: The document explains that when the gesture reader identifies a gesture, the touch in the uitouchphaseended phase will be delayed and sent to the hit-test view. After the gesture recognition is successful, send the original End message to hit-test view cancel when the gesture recognition fails. This example is provided to identify the uitapgesturerecognizer object in the double-click operation. Its numberoftapsrequired is set to 2. If the delaystouchesended is no, the call sequence in the hit-test view is
      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:
      However, this is not the case in actual tests. The actual test result is that if delaystouchesended is no, the call sequence is:
      Touchesbegan: withevent :,
      Touchesended: withevent :,
      Tapgesturerecognizer detected double-click

      If delaystouchesended is yes, the call sequence is:
      Touchesbegan: withevent :,
      Touchesended: withevent :,
      Tapgesturerecognizer detected double-click
      Touchescancelled: withevent:
      This problem has not been clarified yet!

      3. Relationships between multiple gesture recognizer

      You can bind multiple gesture recognizer to a view. By default, touch events in the touch sequence are transmitted in uncertain order in each gesture recognizer, until the event is finally sent to hit-test view (if it is not identified and intercepted by gesture recognizer ). The relationships between multiple gesture recognizer can also be customized as needed, mainly including the following actions

      1. If one of the gesture recognizer fails, the other gesture recognizer can analyze the event.

      For example, two gesture recognizers are used to identify the click and double-click operations, singletapgesture and doubletapgesture respectively. By default, When you click an operation, singletapgesture identifies a click operation. doubletapgesture also identifies a double-click action, but our intention is, this is just a double-click operation. In this case, we can use the identifier of uigesturerecognizer: Method to enable singletapgesture to analyze events when doubletapgesture identifies and identifies events. If doubletapgesture identifies double-click events, singletapgesture will not take any action.

       
      [Singletapgesture requiregesturerecognizertofail: doubletapgesture];

      Note that, in this case, if you perform a click operation, it takes a delay (that is, doubletapgesture failed to identify), singletapgesture will recognize the click action and perform click processing, during this period of time, there was almost no impact on actual use.

      2. Precisely control whether gesture recognizer responds to an event or event sequence.

       

      There are two optional methods in the uigesturerecognizerdelegate protocol to control whether gesture recognizer needs to recognize certain events

        • Gesturerecognizershouldbegin:
          This method is called when the gesture recognizer view is transferred out of the uigesturerecognizerstatepossible state. If no is returned, the method is switched to uigesturerecognizerstatefailed. If yes is returned, the touch sequence is recognized)
        • Gesturerecognizer: shouldreceivetouch:
          This method is called before the touchesbegan: withevent: Method of gesture recognizer when a touch event occurs in the window object. If no is returned, gesture recognizer will not see this touch event. (Yes by default ).

      In addition, there are two rewrite methods in the uigesturerecognizer class to complete the same functions as the delegate method.
      -(Bool) canpreventgesturerecognizer :( uigesturerecognizer *) preventedgesturerecognizer;
      -(Bool) canbepreventedbygesturerecognizer :( uigesturerecognizer *) preventinggesturerecognizer;

      3. Multiple gesture identifiers can be recognized together

      By default, two gesture recognizers do not recognize their gestures at the same time, but you can implement the uigesturerecognizerdelegate protocol in
      Gesturerecognizer: shouldrecognizesimultaneouslywithgesturerecognizer: method to control it. This method is called when either of the two gesture recognizers blocks Another touch event. If yes is returned, both gesture recognizers can recognize the event at the same time. If no is returned, it is not guaranteed that two gesture recognizers cannot be recognized at the same time, because this method of another gesture recognizer may return YES. That is to say, if either of the two gesture recognizers delegate Methods returns Yes, both of them can be recognized at the same time. Only when both return no is mutually exclusive. By default, no is returned.

       

      In this example, if you want to detect all the touch events on the window, you can associate gesture recognizer with the window. By default, if the gesture is recognized by the window, the gesture recognizer in the sub-view becomes invalid, while the goal of gesture recognizer on the window is to monitor all events, but not to handle these events, the processing of specific events also requires various gesture recognizer in the subview to process the events. In this way, we can bind the gesture recognizer delegate method on the window so that gesturerecognizer: Yes is returned.

      -(Bool) gesturerecognizer :( uigesturerecognizer *) gesturerecognizer restart :( uigesturerecognizer *) othergesturerecognizer {return yes ;}

       

      Iv. Similar uiscrollview Behavior

      The scroll view does not have a scroll bar. When there is a touch behavior on the scroll view, it must identify whether the scroll view itself is a child view of its content. To customize how scrollview handles this situation, see the following attributes and methods of the uiscrollview class.
      -Touchesshouldbegin: withevent: incontentview:
      -Touchesshouldcancelincontentview:
      Cancancancelcontenttouches
      Delayscontenttouches

      reference:
      event handling guide for iOS-gesture recognizers
      uigesturerecognizer class reference
      define 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 when in uiscrollview
      Why is scrolling A uitableview much more responsive than scrolling A uiscrollview?
      How to cancel touches exactly like uiscrollview?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.