iOS 子视图屏蔽父视图手势

父视图要实现手势协议UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  if ([touch.view isDescendantOfView:想要哪个子视图屏蔽该手势]) {
    return NO;
  } else {
    return YES;
  }
}

swift

class ViewController: UIViewController {
    var subView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let gesture = UITapGestureRecognizer(target: self, action: #selector(handleGesture))
        gesture.delegate = self
        self.view.addGestureRecognizer(gesture)
        
        let subView = UIView(frame: CGRectMake(100, 100, 100, 100))
        self.subView = subView
        subView.backgroundColor = UIColor.yellowColor()
        self.view.addSubview(subView)
    }
    
    func handleGesture(gesture: UITapGestureRecognizer) -> Void {
        print("AAAA")
    }
}

extension ViewController: UIGestureRecognizerDelegate {
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
        if touch.view?.isDescendantOfView(self.subView) == true {
            return false
        }
        return true
    }
}

 

你可能感兴趣的:(iOS)