Response chain mechanism is a very important concept in the development, in the processing of some events need to have a deep understanding of the delivery of the response chain, we can have better control over the transmission of events. Today we continue to study the response chain and implement a very simple function. The sample code has been uploaded to Https://github.com/chenyufeng1991/HitTest and can be downloaded for debugging. A very simple function to implement is to allow the bottom view to respond to click events through the top view, which also responds to applications that are very important in the chain's use. Some of the different cases are also covered below. The most commonly used methods are hittest:withevent and pointinside:withevent.
"Overriding child views"
。
The gray area is the bottom view, the yellow area is the top view, and I've added click gestures to both views. Now I'm using the Hittest:withevent: method to control the response chain, whether I click on the yellow or gray area, and the bottom view responds. Where the yellow area is a child view of the gray area.
(1) I customize the top yellow view, inherit from UIView, and then rewrite HitTest, implemented as follows:
#import "RewriteView.h" @implementation rewriteview-(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) event{ UIView *view = [Super Hittest:point withevent:event]; if (view = = self) { return nil; } return [Super Hittest:point withevent:event];} @end
Simply explain the following code:
When view = = Self, it indicates that the click Area is on the Rewriteview, where the yellow area is the object, and then returns nil, indicating that it does not respond and gives it to the parent view to respond. Because the parent view here is a gray area, the parent view responds to the click.
(2) VC in the implementation of the following:
#import "RewriteTopViewController.h" #import "Masonry.h" #import "RewriteView.h" @interface Rewritetopviewcontroller ( ) @end @implementation rewritetopviewcontroller-(void) viewdidload{[Super Viewdidload]; UIView *bottomview = [[UIView alloc] initwithframe:cgrectmake (100, 100, 200, 200)]; Bottomview.backgroundcolor = [Uicolor Graycolor]; Bottomview.tag = 101; [Self.view Addsubview:bottomview]; [Bottomview mas_makeconstraints:^ (Masconstraintmaker *make) {Make.center.equalTo (Self.view); Make.width.equalTo (@200); Make.height.equalTo (@200); }]; UITapGestureRecognizer *bottomtap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector ( Tapbottomview:)]; [Bottomview Addgesturerecognizer:bottomtap]; Rewriteview *topview = [[Rewriteview alloc] initwithframe:cgrectmake (100, 100, 100, 100)]; Topview.backgroundcolor = [Uicolor Yellowcolor]; Topview.tag = 102; [Bottomview Addsubview:topview]; [TopView mas_makeconstraints:^ (MASconstraintmaker *make) {make.center.equalTo (Bottomview); Make.height.equalTo (@100); Make.width.equalTo (@100); }]; UITapGestureRecognizer *toptap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Taptopview:)]; [TopView Addgesturerecognizer:toptap];} -(void) Tapbottomview: (ID) sender{NSLog (@ "%s", __function__);} -(void) Taptopview: (ID) sender{NSLog (@ "%s", __function__);} @end
Note that TopView is defined by Rewriteview in your code, and Bottomview is defined by UIView. Running the above code, we will find that all the last click events fall in the gray bottom area, which is equivalent to the yellow area. The yellow zone must inherit from UIView, and then override the HitTest method.
--------------------------------------------------------------------------------------------------------------- ------------------------------------------
1. For touch events, first to find the object that can respond to the event, iOS is to use hit_testing to find which view is touched, that is, Keywindow as the starting point, hit-test view as the end point, Cascade calls Hittest:withevent. The great God on the Internet drew the following picture:
Process flow of the Hittest:withevent method:
Call Pointinside:withevent First: Determine if the touch point is within the current view.
--If you return yes, all child views of the view call Hittest:withevent, and the order of calls is called from low to High (Top-->bottom).
--If no is returned, then Hittest:withevent returns nil, all branches of the view's sub-views are ignored (indicating that it is not possible to be in the view at all).
If a view's pointinside:withevent: Returns Yes, and all its child views Hittest:withevent: Both return nil, or the view has no child views, the view's hittest:withevent: Return to self.
If the hittest:withevent of the Child view: Returns a non-empty object, then the hittest:withevent of the current view also returns the object, that is, fallback along the original path, eventually hit-test view is passed to Keywindow for response.
The Hittest-withevent: method returns nil, causing itself and all its child views not to be hit-testing discovered and unable to respond to touch events:
Hide (hidden=yes) view;
A view that prohibits user action (userinteractionenabled = NO);
view of the alpha<0.01;
The view exceeds the area of the parent view;
-Since the system passes hittest:withevent: To do the transfer chain to retrieve the Hit-test view, then we can modify the object passed back in one of the loops, thus changing the normal event response chain.
2. The pass of the touch event is passed from the parent control to the child control. That is, the root node of the tree structure is passed recursively to the leaf node.
That is, if the parent control cannot receive touch events, the child controls will not be able to receive touch events.
3. The event is passed from top to bottom (parent control to child control), and the response of the event is from bottom to top (child control to parent control).
4. Why is it called two times for a view,hittest?
Because one time a message is delivered, there is also a response time. is in the stack operation, the parent view first into the stack, after the stack.
5. Regardless of whether the view can handle events, as long as a click on the view will produce an event, it is critical to see who handles the event. That is, if the view cannot handle the event, clicking on the view will still produce a touch event, except that the event will not be processed by the clicked view.
6. If you set the transparency or hidden of the parent control, it directly affects the hidden of the child control's transparency, and if the parent control has a transparency of 0 or hidden=yes, the child controls are not visible. Ibid. 1.
7. Tip: Who wants to be the most suitable view to rewrite the hittest:withevent of his own parent control: The method returns the specified child control (which I will implement below), or overrides its own hittest:withevent: Method return self. However, it is recommended that you return child controls in the parent control's hittest:withevent: As the most appropriate view.
Meaning of 8.return nil:
Hittest:withevent: Return nil means calling the current hittest:withevent: The view of the method is not the appropriate view, and the child control is not the appropriate view. The most appropriate view is the parent control if the sibling control of siblings does not have a suitable view.
9.[self.view Convertpoint:point Fromview:self]
Converts the Self.view coordinate system from self to its own coordinate system. You can then call pointinside manually to determine if it is within a view.
"Overriding parent View"
Overriding the parent view to control response events on a child view is the recommended way.
(1) Rewritesuperview inherit from UIView, rewrite hittest
RewriteSuperView.h
#import <UIKit/UIKit.h> @interface Rewritesuperview:uiview@property (nonatomic, strong) UIView *bottom; @end
Among them, bottom and so on is a reference to pass in.
rewritesuperview.m
#import "RewriteSuperView.h" @implementation rewritesuperview-(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) event{ NSLog (@ "Tag=====%ld", Self.tag); Cgpoint bottompoint = [Self.bottom convertpoint:point fromview:self]; if ([Self.bottom pointinside:bottompoint withevent:event]) { return self.bottom; } return [Super Hittest:point withevent:event];} @end
These lines of code mean to detect if the hit area is in the bottom area, and if so, use bottom to respond to the click, otherwise call the parent class's hittest, and pass the response as normal.
(2) VC implementation
#import "RewriteSuperViewController.h" #import "RewriteSuperView.h" #import "Masonry.h" @interface Rewritesuperviewcontroller () @end @implementation rewritesuperviewcontroller-(void) viewdidload{[Super ViewDidLoad] ; Rewritesuperview *containerview = [[Rewritesuperview alloc] Initwithframe:cgrectmake (0, 0, 320, 568)]; Containerview.backgroundcolor = [Uicolor colorwithwhite:0.667 alpha:1.000]; Containerview.tag = 100; [Self.view Addsubview:containerview]; [Containerview mas_makeconstraints:^ (Masconstraintmaker *make) {Make.edges.equalTo (Self.view); }]; UIView *bottomview = [[UIView alloc] initwithframe:cgrectmake (100, 100, 200, 200)]; Bottomview.backgroundcolor = [Uicolor Redcolor]; Bottomview.tag = 101; [Containerview Addsubview:bottomview]; [Bottomview mas_makeconstraints:^ (Masconstraintmaker *make) {Make.center.equalTo (Containerview); Make.width.equalTo (@100); Make.height.equalTo (@100); }]; UITapGestureRecognizer *boTtomtap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Tapbottomview:)]; [Bottomview Addgesturerecognizer:bottomtap]; UIView *topview = [[UIView alloc] initwithframe:cgrectmake (100, 100, 100, 100)]; Topview.backgroundcolor = [Uicolor Yellowcolor]; Topview.tag = 102; [Containerview Addsubview:topview]; [TopView mas_makeconstraints:^ (Masconstraintmaker *make) {Make.center.equalTo (Containerview); Make.height.equalTo (@200); Make.width.equalTo (@200); }]; UITapGestureRecognizer *toptap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Taptopview:)]; [TopView Addgesturerecognizer:toptap]; Containerview.bottom = Bottomview;} -(void) Tapbottomview: (ID) sender{NSLog (@ "%s", __function__);} -(void) Taptopview: (ID) sender{NSLog (@ "%s", __function__);} @end
Note that only the bottom gray area Containerview is Rewritesuperview, and the red Bottomview reference is passed, allowing it to be detected in HitTest.
(3) Take a look at the implemented view:
。
Where the gray background is the parent view, the yellow is the top view, the red at the bottom, the yellow and red are all gray sub-views, yellow covers the red. Let's look at the view level debug:
。
The result of this case is to click in the yellow area, if the hit area falls in the red area, then the red click Gesture will respond, if it is outside the red area, the yellow click Gesture will respond.
The following figure is an example diagram in Apple's official documentation:
。
The official explanations are as follows:
- The touch is within the bounds of view A, so it checks subviews B and C.
- 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.
- 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 in the view H Ierarchy that contains the touch, so it becomes the hit-test view.
English is very simple, it is not for everyone to translate. You can be based on this picture or combined with UIButton, Uiimageview, UIView and other writing some cases, I believe the response chain will have a deeper understanding.
iOS development-An in-depth understanding of the response chain (Responder Chain) and code examples (ii)