1. 程式人生 > >這個坑,你遇到過嗎?關於UIView上新增一個手勢,導致子檢視為UIButton的無法響應的問題

這個坑,你遇到過嗎?關於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;
}