根据响应链传递在hitTest方法中返回接受事件的视图即可,在自定义的button中重写hitTest方法,其中view==nil表示当前点击的是超出button的子视图view区域,再通过遍历button的子视图subviews,找到其中的UIView即可。反过来UIView上覆盖UIButton,在UIView中重写hitTest方法,把button传进来查找
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (view == nil) {
for (UIView *subView in self.subviews) {
CGPoint p = [subView convertPoint:point fromView:self];
if (CGRectContainsPoint(subView.bounds, p)) {
return self;
}
}
}
return view;
}
或者直接一点,把子view当做参数传进来查找,看点击区域是否在view上
@interface TempButton : UIButton
@property (nonatomic, strong) TempView *tempView;
@end
#import "TempButton.h"
@implementation TempButton
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (view == nil) {
CGPoint p = [self.tempView convertPoint:point fromView:self];
if (CGRectContainsPoint(self.tempView.bounds, p)) {
return self;
}
}
return view;
}
@end
网友评论