Two methods for UIView to respond to events

Reference from: https://blog.csdn.net/mushaofeng1990/article/details/62434349

The event delivery process after the user touches the screen:

 

 

 

 


//方法A
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ [super hitTest:point withEvent:event]; return nil; } //method B -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{ return YES; }

After rewriting the two methods of view, you will find that after clicking the screen, the first response is method A ;

If in method A, we do not call this method of the parent class, then according to the return view of this method A, it is used as the view that responds to the event. (Of course it returns nil, that is, the view does not respond)

 

If in method A, we call this method of the parent class, that is

[ super hitTest :point  withEvent :event]; At this time, the system will call method B ; through the return value of this method, it is judged whether the current view can respond to the message. 

If method B returns no, then there is no need to traverse its subviews. The view returned by method A is the view that can respond to events.

If method B returns YES, then traverse its subviews. (As we described in the above figure, find a suitable view to return, if not, then the view returned by method A will respond to this event.)

 

So to summarize:

//Return a view to respond to the event (if we don't want to affect the event delivery chain of the system, in this method, it is best to call this method of the parent class)

- (nullableUIView *)hitTest:(CGPoint)point withEvent:(nullableUIEvent *)event;

//The returned value can be used to determine whether to continue traversing the subview (the basis for the return is whether the touched point is within the frame range of the view)

 

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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324928533&siteId=291194637
Recommended