★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(let_us_code)
➤博主域名:https://www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/13805736.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Swift:
1 override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? { 2 var view = super.hitTest(point, withEvent: event) 3 if view == nil { 4 for subView in self.subviews { 5 let tp = subView.convertPoint(point, fromView: self) 6 if CGRectContainsPoint(subView.bounds, tp) { 7 view = subView 8 } 9 } 10 } 11 return view 12 }
测试:
1 override func viewDidLoad() { 2 super.viewDidLoad() 3 let costemView = CustomView(frame: CGRectMake(100, 100 , 100, 100)) 4 self.view.addSubview(costemView) 5 6 let button = UIButton(frame: CGRectMake(-20, -20, 40, 40)) 7 costemView.addSubview(button) 8 button.backgroundColor = UIColor.lightGrayColor() 9 button.addTarget(self, action: #selector(aaa), forControlEvents: .TouchUpInside) 10 } 11 12 func test() -> Void { 13 print("swift") 14 }
Objective-C:
1 - (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event { 2 UIView * view = [super hitTest:point withEvent:event]; 3 if (view == nil) { 4 for (UIView * subView in self.subviews) { 5 // 将坐标系转化为自己的坐标系 6 CGPoint tp = [subView convertPoint:point fromView:self]; 7 if (CGRectContainsPoint(subView.bounds, tp)) { 8 view = subView; 9 } 10 } 11 } 12 return view; 13 }