2.把 storyboard里的button连线(我们发现不能连线),这时我们选择 从代码里连线到storyboard上
3.
#import "YellowView.h"
@interface YellowView ()
@property (nonatomic, weak) IBOutlet UIButton *btn;
@end
@implementation YellowView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// 当前坐标系上的点转换到按钮上的点
CGPoint btnP = [self convertPoint:point toView:self.btn];
// 判断点在不在按钮上
if ([self.btn pointInside:btnP withEvent:event]) {
// 点在按钮上
return self.btn;
}else{
return [super hitTest:point withEvent:event];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s",__func__);
}
上面代码在自定义UIView类里,覆盖hitTest:方法, 实现穿透上层点击下面的button的效果。
演示: