我有一个集合视图/滚动视图,我在其中添加我的 UILongPressGestureRecognizer。它可以工作,但是 state .Began 只会与 state 一起被触发。结束。这对我不起作用,因为我还想跟踪状态 .Changed 以获取gesture.locationInView。
我发现的问题是它与 collection view 的 Scroll 手势冲突。
如果我将 scrollEnabled 设置为 false,一切都会按预期进行。
在我看来DidLoad 我有
let longPressedGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(PhotosViewController.Scrubber(_:)))
self.scrubberCollectionView.addGestureRecognizer(longPressedGestureRecognizer)
longPressedGestureRecognizer.minimumPressDuration = 0.6
longPressedGestureRecognizer.delegate = self
self.scrubberCollectionView.addGestureRecognizer(longPressedGesture)
我还尝试添加这些以尝试在启用平移手势时使一个手势失败......(我也尝试了 shouldRecognizeSimultaneouslyWithGestureRecognizer。
longPressedGestureRecognizer.requireGestureRecognizerToFail(scrubberCollectionView.panGestureRecognizer)
func overridePan(gestureRecognizer: UILongPressGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: UIPanGestureRecognizer ) -> Bool {
return (gestureRecognizer == longPressedGesture && otherGestureRecognizer == self.scrubberCollectionView.panGestureRecognizer )
}
……无济于事。
对于我的主要功能
func Scrubber(gesture: UILongPressGestureRecognizer) {
if (gesture.state == .Began ) {
print("Began")
}
else if (gesture.state == .Changed) {
print("Changed")
}
else if (gesture.state == .Ended){
print("Ended")
}
}
当我长按时,我在触地时没有收到任何事件,然后在触地时我得到以下结果:
Began
Ended
实际的长按正在工作,如果我点击则不会调用 Scrubber 函数,它只是在 Ended 状态下同时触发 Began 和 Ended。
并将这些东西添加到 viewDidLoad 中,以尝试延迟滚动视图的触摸,甚至完全取消它……</p>
self.scrubberCollectionView.delaysContentTouches = true
let longPress = UILongPressGestureRecognizer()
func gestureRecognizer(gestureRecognizer: UILongPressGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if(touch.view == scrubberCollectionView && gestureRecognizer == longPress){
return true
}else{
return false
}
}
…没事了。