Click to respond to the Hit-testview

Source: Internet
Author: User

Preface: Why can't my uitextview slide?

"When a user triggers an event (Touch event or Motion event), Uikit creates an event object (Uievent) that contains some of the information needed to handle the event. The event object is then placed in an event queue. These events are processed in FIFO order. When an event is processed, the program's UIApplication object takes an event object out of the queue's head and distributes it. Usually the first is to distribute the event to the main window object of the program, and for touch events, the Window object first attempts to distribute the event to the view on which the touch event occurred . This view is often referred to as the hit-test view , and the process of finding this view is called hit-testing.

The system uses hit-testing to find the view under touch, which detects whether a touch event occurs within the bounds of the corresponding view object (that is, the frame property of the view, which is why the child view cannot respond to the event if it is outside the frame of the parent view). If it is, it will recursively detect all of its child views. The bottom-most view in a view hierarchy that contains touch points is the hit-test view. After the Hit-test view is detected, the system sends the event to this view for processing.

We demonstrate the hit-testing process through an example. Figure 1 is a view hierarchy,

Assuming that the user clicked on view e, the system finds the Hit-test view in the following order:

    1. Click events occur within the bounds of view a, so detect sub-views B and C;
    2. The Click event is not within the bounds of view B, but is within the bounds of view C, so it detects sub-images D and E;
    3. The Click event is not within the bounds of view D, but within the bounds of view e;

View E is the bottommost view (inverted tree structure) in the view hierarchy that contains the touch point, so it is the hit-test view.

The Hit-test view can handle the touch event first, and if the Hit-test view cannot handle the event, the event is passed up the response chain until it finds a view that can handle it.

Event delivery

The object that most has the opportunity to handle the event is the Hit-test view or the first responder. If neither of these handles the event, Uikit will pass the event to the next responder in the response chain. Each responder determines whether it will handle the event or pass it to the next responder by means of the Nextresponder method. This process continues until a responder object is found that can handle the event, or the responder is eventually not found.

Figure 2 illustrates the process of such an event passing,

When an event is detected by the system, it is passed to the initial object, which is usually a view. The event is then handled in the following path (let's take the example on the left image):

    1. The initial view (initial view) attempts to handle the event. If it cannot handle the event, the event is passed to its parent view.
    2. The parent view of the initial view (Superview) attempts to handle the event. If the parent view cannot handle the event yet, continue to pass the view to the upper view.
    3. The upper view (topmost views) attempts to handle the event. If the upper view is still unable to handle the event, the event is passed to the view controller where the view resides.
    4. The view controller tries to handle the event. If the view controller cannot handle the event, the event is passed to the Window object.
    5. The Window object attempts to handle the event. If not, the event is passed to the Singleton app object.
    6. If the app object cannot handle the event, the event is discarded.

"From http://southpeak.github.io/blog/2015/03/07/uiresponder/

In my case, I added a subview called Uiimageview F in the viewb of the example above, and then added a subview called Uitextview G (such as) on Uiimageview F.

Because of the userinteractionenabled of Uiimageview; default is no, so when you go from a to branch B, on the branch E of B, because userinteractionenabled is no, it returns nil. This way, Uitextview will not respond to the click events I have slipped.

There are a few more to note:

Ps.

1, the default hit-testing order is according to UIView in the inverse order of subviews

2. If there are overlapping parts in the same level subview of the view, the top subview is checked, if the top Subview returns nil, then the subview at the bottom is checked

3, Hit-test is also relatively clever, the detection process has such a point, that is, if the click does not occur in a view, then the event can not occur in the view of the subview, so the detection process found that the event is not in Viewb, Also directly will not detect within the VIEWF. That is, if your subview is set to Clipstobounds=no, the actual display area may go beyond the superview frame, and you click Beyond the section and will not process your event , is so capricious!

The check mechanism of the hit-test is as shown above, when Hit-testview is determined, if the current application does not ignore touch events (uiapplication:isignoringinteractionevents), Then application will distribute the event (sendevent:->keywindow:sendevent:)

The UIView provides two methods to determine the Hit-testing View, as shown below- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; // recursively calls -pointInside:withEvent:. point is in the receiver‘s coordinate system

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event; // default returns YES if point is in bounds

When a view receives a hittest message, it calls its own pointinside:withevent: method, if Pointinside returns Yes, indicates that the touch event occurs within myself, and then iterates through all of its subview to find the smallest unit ( No child view) of the UIView, if current view.userinteractionenabled = no,enabled=no(Uicontrol), or alpha<= 0.01, hidden and other situations, HitTest will not call their own pointinside, directly return nil, and then the system went back to traverse the sibling node.

"From Http://suenblog.duapp.com/blog/100031/iOS%E4%BA%8B%E4%BB%B6%E5%88%86%E5%8F%91%E6%9C%BA%E5%88%B6%EF%BC%88%E4 %b8%80%ef%bc%89%20hit-testing

Click to respond to the Hit-testview

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.