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的效果。
示範: