- 作用:专门来寻找最合适的view
- 什么时候调用:当一个事件传递给当前的View时候调用.
- 返回谁,谁就是最合适的View
-(UIView *)hitTes:(CGPoint)point withEvent:(UIEvent *)event
{
//判断自己是否能接受事件
if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01)
{
return nil:
}
// 判断当前点 在不在自己身上.
if (![self pointInside:point withEvent:event]) {
return nil;
}
// 查看自己是不是最合适的view,从后往前遍历自己的子控件.
int count = (int)self.subviews.count;
for (int i = count -1 ; i >= 0; i--) {
UIView *childView = self.subviews[i];
CGPoint childP = [self convertPoint:point toView:childView];
UIView *view = [childView hitTest:childP withEvent:event];
if (view) {
return view;
}
}
return self;
}
网友评论