Hittest method
- It’s used to find the best view
- When an event is passed to a control, the control’s hitTest method is called
- Click on the white view: Touch event -> UIApplication -> UIWindow call [UIWindow hitTest] -> White View [WhteView hitTest]
Experiment 1:
Define a BaseView in which you implement a method, touchBegan, that listens for which class is currently calling the method.
Define a KeyWindow and implement a hitTest method in it that listens for which class called the method and uses it to track which view is the most appropriate view
Add 5 views of different colors on the interface of the controller, each view custom view to achieve, so the gesture on different views can be intercepted by different views.
|
|
Results:
Click on white 1:
|
|
Click on blue 3:
|
|
So what hitTest does is find the most appropriate view, so we can specify the most appropriate view for anything as a particular view
Experiment 2:
If you return the BlueView from the hitTest method in the KeyWindow, clicking on any color block of view will be handed to the BlueView to handle the event.
|
|
Results:
|
|
Because the event responder chain is that when the user manipulates the screen, an event will be generated, and the event will be added to the event queue by the system. The UIApplication object will pass the earliest event added to the event queue to the Window, and then the Window will find the most appropriate view to process the event. So any event will be judged first by the KeyWindow object to find the most appropriate view
Two important ways
-
-(BOOL)pointInside:(CGPoint) Point withEvent:(UIEvent *) Event: Indicates whether the touch point is on the control
-
-(UIView *)hitTest:(CGPoint) Point withEvent:(UIEvent *)event: Used to determine whether the control accepts the event and find the most appropriate view
Imitate the system implementation to find the most appropriate view
|
|
Experiment:
Add a UIButton and then a custom UIView(ShelterView) on top of the button.
Requirement: Click on a point on the ShelterView, give it to THE UIButton if it’s also in the UIButton range, give it to the ShelterView if it’s not on the UIButton, and give it to the controller’s view if you click on a point on the screen other than the ShelterView.
|
|
Github.com/FantasticLB…