在做弹出层时的总结(拦截手势的action)

在做弹出层时的总结:

如果自定义view上面加了手势(以tap为例),那么在其上面添加继承UIView的类(如view、label、toolbar等)的控件的话,点击后是照样能捕捉到手势的行为,由于响应者链的缘故,是可以将tap行为传递到view上面的任一层继承UIView的控件。这个时候需要对tap进行拦截,有以下两种做法:

(1)在touchesBegan方法中进行拦截;

eg:override func touchesBegan(touches: Set, withEvent event: UIEvent?) {

guard let touch = touches.first else {return}

let curP = touch.locationInView(self)

QL1(curP)

if curP.y < bottomView.frame.origin.y {

hide()

}

}

(2)在hittest方法中进行判断拦截;

eg:

/// 拦截手势

override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {

// 判断点在不在添加的底部区域上面

let tapP = convertPoint(point, toView: bottomView)

if bottomView.pointInside(tapP, withEvent: event) {

return super.hitTest(point, withEvent: event)

} else {

hide()

return super.hitTest(point, withEvent: event)

}

}

你可能感兴趣的:(在做弹出层时的总结(拦截手势的action))