触地按钮(子视图)时,不要在超级视图上处理平移手势

时间:2016-04-26 09:38:11

标签: ios objective-c swift view uigesturerecognizer

我有一个视图(superview),我可以通过平移手势移动,此视图具有带TouchDown事件的子视图(UIButton)。但是当我按下按钮(触摸事件处理)并继续移动手指我的superview开始处理平移手势方法。 当我触及其子视图时,如何禁用处理超级视图的平移手势方法?

1 个答案:

答案 0 :(得分:0)

要在UIView的子视图中停用触摸事件,您可以将此扩展程序添加到UIViewController

extension UIViewController:UIGestureRecognizerDelegate {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        view.addGestureRecognizer(tap)
        tap.delegate = self
    }

    func dismissKeyboard() {
        view.endEditing(true)
    }
    public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
       if (touch.view!.isDescendantOfView(self.view) && touch.view != self.view){
            return false
        }
        return true
    }
}