IOS development diary 33-hit-Test, ios diary 33-hit-test

Source: Internet
Author: User

IOS development diary 33-hit-Test, ios diary 33-hit-test

Today, the blogger has an hitHest requirement and encountered some difficulties. I would like to share with you the hope that we can make common progress.

When we click the interface, how does iOS know which View we click?

This process is completed by hit-testing. You can use the hit-testing app to know which view is used to respond to events.

Next I will briefly introduce how hit-testing works. When a touch or other gesture occurs on the interface, UIKit will Package A UIEvent object and pass the object to the currently active app, after being distributed to the app, the simple UIApplication retrieves an event from its event queue for response.

Then the UIApplication will begin to worry about which View to respond to this event, so this is the time for hit-testing to appear.

The execution process of hit-testing is: after the UIApplication receives the UIEvent, it will send the event to the UIWindow, and then the UIWindow will send the event to its SubView, then, pass it to the SubView again to determine whether the View is in this View until the View with the smallest occurrence of this event is found.

If not found, return to itself, and then start searching again from the sibling View. But the question is, in what order does hit-testing look for SubView. You add the SubView to traverse in reverse order. In other words, you can find the SubView at the top level.

Note:


View.png

The order in which I add views is:

[self.view addSubView:View1];[self.view addSubView:View2];[self.view addSubView:View3];

Therefore, the sequence of hit-testing detection SubView is:


Hit-testing detection sequence .png


Therefore, hit-testing performs depth-first detection. Of course, it is not brainless depth-first. If View3 has its own SubView, hit-testing will not detect the SubView of view3.

When View3 is detected, it can be determined that the event occurrence point is not within View3, so its SubView will not be detected. Therefore, hit-testing will be trimmed during detection to improve efficiency.

How can I implement the hit-testing detection process in the code? Of course, it is impossible for us to know exactly what we guess.

There are two methods in UIView:

- (BOOL)pointInside:(CGPoint)*point* withEvent:(UIEvent *)*event;- (UIView *)hitTest:(CGPoint)*point* withEvent:(UIEvent *)*event;

Hit-testing is called

- (UIView *)hitTest:(CGPoint)*point* withEvent:(UIEvent *)*event;

To obtain the smallest UIView of an event, but by calling

- (BOOL)pointInside:(CGPoint)*point* withEvent:(UIEvent *)*event;

To determine whether an event occurs in a UIView. Therefore, after the UIView receives the hit-testing message, it first determines its alpha, userInteractionEnabled, and hidden attributes. If these attributes do not meet the requirements

- (UIView *)hitTest:(CGPoint)*point* withEvent:(UIEvent *)*event;

Nill is returned directly. If yes, nill is used.

- (BOOL)pointInside:(CGPoint)*point* withEvent:(UIEvent *)*event;

Determine whether an event occurs on your own. If it is not on your own, nill is returned. If it is on your own, the SubView is called.

- (UIView *)hitTest:(CGPoint)*point* withEvent:(UIEvent *)*event;

To get a View and return it.

So far, the specific process of hit-testing has been completed. What are the advantages of hit-testing? In the subclass of UIView, We can override

- (UIView *)hitTest:(CGPoint)*point* withEvent:(UIEvent *)*event;

So there are more interesting things.

Related Article

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.