核心方法
// 事件传递
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
// 在不在里 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
hitTest底层实现底层实现
1.判断当前能不能接收事件
if(self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01)
returnnil;
2.判断触摸点在不在当前的控件上
if(![self pointInside:point withEvent:event]) return nil;
3.从后往前遍历自己的子控件
int count = (int)self.subviews.count;
for (int i = count - 1; i >= 0;i-- ) {
UIView *childV = self.subviews[i];
把当前坐标系上的点转换成子控件坐标系上的点.
CGPoint childP =[self convertPoint:point toView:childV];
判断自己的子控件是不是最适合的View
UIView *fitView =[childV hitTest:childP withEvent:event];
如果子控件是最适拿的View,直接返回
if (fitView) {
returnfitView;
}
}
4.自己就是最适合的View
return self.
image.png