iOS 解决父视图点击事件导致子视图事件不触发


如图,是蓝色父视图blueView添加tap事件,内部红色子视图redView的tap事件将会无法触发
解决办法

  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemCLick)];
  tap.delegate = self;
  [self.blueView addGestureRecognizer:tap];  
  [self.blueView addSubView:self.redView];

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    //解决点击子视图父视图事件相应的问题
    CGPoint point = [gestureRecognizer locationInView:self.blueView];//获取手势点击坐标
    if(CGRectContainsPoint(self.redView.frame, point)) {
        //判断点是否在子视图范围内
        return NO;//父视图点击事件不响应
    }
    return  YES;//父视图点击事件响应
}
posted @ 2023-10-27 09:48  qqcc1388  阅读(176)  评论(0编辑  收藏  举报