这个坑,你遇到过吗?关于UIView上添加一个手势,导致子视图为UIButton的无法响应的问题

问题描述:
1.view上添加了一个UITapGestureRecognizer
2.view上添加了一个button
3.在iOS 6.0+上测试一切正常,在ios5.0(5.1)上UIButton只能点击但是就是不进入target事件之中


问题解决:
首先让UITapGestureRecognizer不要吃掉子窗口的事件:
tapGesture.cancelsTouchesInView = NO;

然后设置自身的代理:
tapGesture.delegate = self;

最后实现代理里面这个方法:
// called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch. return NO to prevent the gesture recognizer from seeing this touch
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
        // 过滤掉UIButton,也可以是其他类型
    if ( [touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    
    return YES;
}


你可能感兴趣的:(iOS开发)