To have a child view that is outside the parent view range respond to an event and respond to clicks outside of the UIView range

Source: Internet
Author: User

Override this method to allow a child view that is out of the parent view range to respond to an event
-(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) Event {
UIView *view = [Super Hittest:point withevent:event];
if (view = = nil) {
For (UIView *subview in self.subviews) {
Cgpoint TP = [SubView convertpoint:point fromview:self];
if (Cgrectcontainspoint (subview.bounds, TP)) {
view = SubView;
}
}
}
return view;
}

If a button has a part that is outside the scope of the parent control, this part cannot respond to the click, and if you want it to respond to the click what should be donewords 536 Read 488 comments 6 likes 5

The requirements in the headline are often met, such as


Screen shot 2016-04-08 pm 1.07.00.png

When the button exceeds the tab bar view, then the part that the button exceeds is not clickable. Well, first, the solution.

1. We rewrite the Blue View-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *) Method of Event

  - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{    //if内的条件应该为,当触摸点point超出蓝色部分,但在黄色部分时    if (.....){ return YES; } return NO; }
So why is the above solution possible?

This is related to the iOS event distribution mechanism hit-testing, simply put, the role of hit-testing is to find out every time you touch the screen, the point to exactly which view.

such as the following figure


Screen shot 2016-04-08 pm 1.38.09.png

When I clicked on View-c, hit-testing actually detected this.
1. First, the view starts with View-a and discovers that the touch point is view-a, so check view-a's child view View-b.
2. Find the touch point inside the view-b, great! Look at the sub-view view-c within the view-b.
3. The touch point is found inside the view-c, but View-c has no child view, so view-c is the hit-testview of this touch event.

So UIView actually provides two ways to determine Hit-testview
1.-(UIView ) HitTest: (cgpoint) point withevent: (Uievent ) event;
2.-(BOOL) Pointinside: (cgpoint) point withevent: (uievent *) event;//This is the method we've rewritten above.

Notice that in fact, before each recursive call to HitTest: (cgpoint) point withevent: (Uievent *) event, Pointinside:withevent is called to determine whether the touch point is within that view.

So when we rewrite pointinside: (cgpoint) point withevent: (Uievent *) After the event, actually we call HitTest after the click to recursively find the Hit-testview area from this:
Became like this:

So when we click on the upper half-raised area happily, hit-testing goes back to check the child view in the Blue view, the yellow area. To complete this touch event.
Enjoy:)

To have a child view that is outside the parent view range respond to an event and respond to clicks outside of the UIView range

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.