zoukankan      html  css  js  c++  java
  • [iOS]解决:子视图超出父视图不响应问题

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(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 }
  • 相关阅读:
    vtk 矩阵管理系统
    在opengl中使用纹理
    [译文]:单元测试的七种境界
    [翻译]:六分钟八法则塑造优秀程序员
    weekly review 200921: Power Sleep
    Bye, Scofield
    weekly review 200922: Goal
    weekly review 200920: Prototype Demo
    转载:测试驱动开发三原则
    weekly review 200918: productive
  • 原文地址:https://www.cnblogs.com/strengthen/p/13805736.html
Copyright © 2011-2022 走看看