开发者社区> 问答> 正文

父View范围内的button可点,范围外不可点,为什么?

不废话,直接上代码

    self.back = [[UIView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 100.0f, 100.0F)];
    self.back.backgroundColor = [UIColor redColor];
    [self.window addSubview:self.back];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 200, 40)];
    [btn addTarget:self action:@selector(zouni) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor blackColor];
    [self.back addSubview:btn];

效果如下
screenshot
红色的方块是back,在这个范围内的btn可以点,点了 - (void)zouni 是有反应的,外边的没反应。
求解释,求原理,还有什么办法可以让整个btn可以点?

展开
收起
a123456678 2016-07-20 11:48:35 1780 0
1 条回答
写回答
取消 提交回答
  • 创建一个UIView的子类,并且重写这个子类的

    • (UIView )hitTest:(CGPoint)point withEvent:(UIEvent )event
      方法,在这个方法里,判断point参数是否在你的btn的范围内,如果是,则返回btn
    2019-07-17 19:58:46
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载