ios代码如何让子类视图超出父类视图的部分响应事件
在父视图重写方法
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (self.userInteractionEnabled == NO && self.alpha <= 0.01 && self.hidden == YES) {
return nil;
}
for (UIView * subview in self.subviews.reverseObjectEnumerator) {
CGPoint converP = [subview convertPoint:point fromView:self];
UIView *suitableView = [subview hitTest:converP withEvent:event];
if (resultView) {
return suitableView;
} else {
return view;
}
}
return view;
}
//重写hit让超出self范围的子视图能响应点击时间
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if (self.userInteractionEnabled == NO && self.alpha<=0.01 && self.hidden == YES) {
return nil;
}
//根据返回的view是否为空判断点击的是否是在父视图的范围内,null则表示不再范围内,否则返回自己本身。
UIView *view = [super hitTest:point withEvent:event];
if (!view) {
for (UIView *subView in self.subviews) {
CGPoint converPoint = [subView convertPoint:point fromView:self];
//点是否在相应的视图上
if (CGRectContainsPoint(subView.bounds, converPoint)) {
//相应的视图处理事件并返回
return [subView hitTest:converPoint withEvent:event] ;
}
}
}
return view;
}