在我的ViewController中,我有一个用于切换视图的UIScreenEdgeGestureRecognizer
,还有一个带有自定义UITableView
的UITableViewCell
。在这个自定义中,UITableViewCell
是一个用于滑动单元格的UIPanGestureRecognizer
。
我添加了gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
这样两个手势都可以工作,并且gestureRecognizerShouldBegin
可以防止垂直滚动的冲突。
问题是,我如何才能赋予UIScreenEdgeGestureRecognizer
优先级?当我在屏幕边缘滑动时,我想在不平移单元格的情况下切换视图。我想我应该使用委托方法,但到目前为止,我读得越多,我就越困惑。
自定义代码:
override func viewDidload() {
var recognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
recognizer.delegate = self
addGestureRecognizer(recognizer)
}
override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if let panGesture = gestureRecognizer as? UIPanGestureRecognizer {
let velocity = panGesture.velocityInView(superview!)
if fabs(velocity.x) > fabs(velocity.y) { return true }
return false
}
return false
}
视图控制器:
override func viewDidLoad() {
let rightScreenEdgeRecognizer = EdgePanGesture(target: self, action: "changeView:")
rightScreenEdgeRecognizer.edges = .Right
view.addGestureRecognizer(rightScreenEdgeRecognizer)
}
我还试着用以下方法识别识别器:
gestureRecognizer.isKindOfClass(UIScreenEdgePanGestureRecognizer)
和
gestureRecognizer as? UIScreenEdgePanGestureRecognizer
但都失败了。
最佳答案
结果发现我用gestureRecognizer as? UIScreenEdgePanGestureRecognizer
看的是正确的方向,但是我应该用gestureRecognizer
代替otherGestureRecognizer
。
override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if let recognizer = otherGestureRecognizer as? UIScreenEdgePanGestureRecognizer {
return true
}
return false
}
关于ios - 检测UIScreenEdgeGestureRecognizer并使UIPanGestureRecognizer失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29461964/