Hittest:withevent and Responder Chain

Source: Internet
Author: User

This method is to find the view is touch, when found to become the first of the response chain, if he can not handle the event, then find Nextresponder until application if not processed, it will be discarded.

Https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_ Delivery_responder_chain/event_delivery_responder_chain.html

The following is an official explanation.

Event delivery:the Responder Chain

When you design your app, it's likely that's want to respond to events dynamically. For example, a-touch can occur in many different objects onscreen, and you had to decide which object you want to respond To a given event and understand how that object receives the event.

When a user-generated event occurs, UIKit creates an event object containing the information needed to process The event. Then it places the event object in The active app" S event queue. For touch events, the object is a set of touches packaged in a  uievent  object. For motion events, the event object varies depending on the which framework of your use and what type of motion event is you inte Rested in.

an event travels along a specific path until it's delivered to an object that can handle it. First, The singleton  uiapplication  object takes an event from the top of the queue and dispatches it for HA Ndling. Typically, it sends the event to the app ' s Key window object, which passes the event to a initial object for handling. The initial object depends on the type of event.

    • Touch Events. for touch events, the Window object first tries to deliver the event To the view where the touch occurred. That view is known as the Hit-test view. The process of finding the hit-test view is called  hit-testing , which is described In hit-testing Returns the View Where a Touch occurred.

    • Motion and remote control Events. with These events, the Window object sends the Shaking-motion or Remote control event to the first responder for handling. The first responder is described In the responder Chain are made up of responder objects.

The ultimate goal of these event paths is to find a object that can handle and respondto an event. Therefore, UIKit first sends the event to the object that's best suited to handle the event. For touch events, this object is the Hit-test view, and for other events, this object is the first responder. The following sections explain in more detail how the Hit-test view and first responder objects is determined.

Hit-testing Returns the View Where a Touch occurred

IOS uses hit-testing to find the view is under a touch. Hit-testing involves checking whether a touch is within the bounds of any relevant view objects. If It is, it recursively checks all of the that view ' s subviews. The lowest view in the view hierarchy that contains, the touch point becomes the hit-test view. After IOS determines the hit-test view, it passes the "touch event to the" View for handling.

To illustrate, suppose this user touches view E in Figure 2-1. IOS finds the Hit-test view by checking the Subview s in this order:

    1. The touch is within the bounds of view A, so it checks subviews B and C.

    2. The touch isn't within the bounds of view B, but it's within the bounds of view C, so it checks subviews D and E.

    3. The touch isn't within the bounds of view D, but it's within the bounds of view E.

      View E is the lowest view of the view hierarchy that contains the touch, so it becomes the hit-test view.

Figure 2-1 Hit-testing Returns the Subview it was touched

the  hittest:withevent:  method Returns the hit test view for a given  cgpoint  and  uievent . the  hittest:withevent:  method begins by calling the  pointinside:withevent:  method on itself. If the point passed into  hittest:withevent:  is inside the bounds of the view,  Pointinside:withevent:  returns  YES . Then, the method recursively calls  hittest:withevent:  on every subview, returns  YES .

If the point passed into hitTest:withEvent: was not inside the bounds of the view, the first call to the pointInside:withEvent: method returns NO , the Point is ignored, and hitTest:withEvent: returns nil . If a subview returns NO , that whole branch of the view hierarchy are ignored, because if the touch did not occur in that Subview, it also did not occur in any of the that Subview ' s subviews. This means, any point in a subview so is outside of it Superview can ' t receive touch events because the touch point Have to is within the bounds of the Superview and the Subview. This can occur if the Subview's property was clipsToBounds set to NO .

Note:a Touch object is associated with its hit-test view for its lifetime, even if the touch later moves outside the view .

The hit-test view is given the first opportunity to handle a touch event. If The Hit-test view cannot handle an event, the event travels up that view's chain of responders as described in the Responder Chain is made up of Responder Objects until the system finds a object that can handle it.

The Responder Chain is made up of Responder Objects

Many types of events rely on a responder chain for event delivery. The responder chain is a series of linked responder objects. It starts with the first responder and ends with the Application object. If The first responder cannot handle an event, it forwards the event to the next responder in the responder chain.

A Responder Object is an object which can respond to and handle events. UIResponderThe class is the base class for all responder objects, and it defines the programmatic interface isn't only for event Handling but also for common responder behavior. Instances of UIApplication The, UIViewController , and UIView classes is responders, which means, all views and most key controller objects is responders. Note that Core Animation layers is not responders.

The first responder is designated to receive events first. Typically, the first responder is a view object. An object becomes the first responder by doing and things:

    1. Overriding the canBecomeFirstResponder method to return YES .

    2. Receiving a becomeFirstResponder message. If necessary, an object can send itself the this message.

Note:make sure that your app have established its object graph before assigning a object to be the first responder. For example, your typically call the becomeFirstResponder method in an override of the viewDidAppear: method. If you try to assign the first responder viewWillAppear: in, your object graph isn't yet established, so the becomeFirstResponder method returns c8/>.

Events is not the only objects this rely on the responder chain. The responder chain is used in all of the following:

  • Touch events. If The Hit-test view cannot handle a touch event, the event is passed up a chain of responders that starts with the Hit-te St View.

  • Motion events.  To handle shake-motion events with UIKit, the first responder must implement either the motionBegan:withEvent: or motionEnded:withEvent: method UIResponder of the Class, as described in detecting shake-motion Events with uievent.

  • Remote control events. To handle remote control events, the first responder must implement the remoteControlReceivedWithEvent: method of the UIResponder class.

  • Action messages. When the user manipulates a control, such as a button or switch, and the target for the action method is nil , the MESSAG E is sent through a chain of responders starting with the control view.

  • Editing-menu messages. When a user taps the commands of the editing menu, IOS uses a responder chain to find an object that implements the Necess ary methods (such as cut: , copy: , and paste: ). For more information, see Displaying and managing the Edit Menu and the sample Code project, copypastetile.

  • Text editing. When a user taps a text field or a text view, that view automatically becomes the first responder. By default, the virtual keyboard appears and the text field or text view becomes the focus of editing. You can display a custom input view instead of the keyboard if it's appropriate for your app. You can also add a custom input view to any responder object. For more information, see Custom views for Data Input.

UIKit automatically sets the text field or text view a user taps to be the first responder; Apps must explicitly set all other first responder objects with the becomeFirstResponder method.

The Responder Chain follows a specific Delivery Path

If The initial object-either the Hit-test view or the first RESPONDER-DOESN ' t handle an event, UIKit passes the Event to the next responder in the chain.   (can not be processed before passing, although the response chain is always present, only the hit-test view or the first RESPONDER-DOESN ' t handle an event, what should be handled? Then find the next responder. ) Each responder decides whether it wants to handle the event or pass it along to its own next RESP Onder by calling the  nextresponder  method. This process continues until a responder object either handles the event or there is no more responders.

The responder chain sequence begins when IOS detects an event and passes it to an initial object, which is typically a vie W. The initial view has the first opportunity to handle an event. Figure 2-2 shows the different event delivery paths for the app configurations. An app's event delivery path depends on it specific construction, but all event delivery paths adhere to the same heurist ICS.

Figure 2-2 the responder chain on IOS

For the app on the left, the event follows this path:

  1. The initial view attempts to handle the event or message. If it can ' t handle the event, it passes the event to its Superview, because the initial view isn't the top most view In its view controller ' s view hierarchy.

  2. The Superview attempts to handle the event. If the Superview can ' t handle the event, it passes the event to its superview, because it's still not the top most view I n the view hierarchy.

  3. The topmost view in the View controller's view hierarchy attempts to handle the event. If the topmost view can ' t handle the event, it passes the event to its view controller.

  4. The view controller attempts to handle the event, and if it is can ' t, passes the event to the window.

  5. If the Window object can ' t handle the event, it passes the event to the Singleton app object.

  6. If the App object can ' t handle the event, it discards the event.

The app on the right follows a slightly different path, but all event delivery paths follow these heuristics:

    1. A View passes an event up its view controller ' s view hierarchy until it reaches the top Most view.

    2. The topmost view passes the event to its view controller.

    3. The view controller passes the event to its topmost view ' s superview.

      Steps 1-3 Repeat until the event reaches the root view controller.

    4. The root view controller passes the event to the Window object.

    5. The window passes the event to the App object.

Important:if implement a custom view to handle remote control events, action messages, shake-motion events with UIKit , or Editing-menu messages, don ' t forward the event or message to nextResponder directly to send it up the responder chain. Instead, invoke the superclass implementation of the current event handling method and let UIKit handle the traversal of T He responder chain for you.

Hittest:withevent and Responder Chain

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.