我有一个收藏视图/滚动视图,我添加了我的UILongPressGestureRecognizer。它有效,但状态.Began只与州一起被解雇。结束。这对我来说不起作用,因为我想跟踪状态。更改为获取gesture.locationInView。
我发现的问题是它与集合视图的Scroll手势发生冲突。
如果我将scrollEnabled设置为false,则一切都按预期工作。
在我的viewDidLoad中我有
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以尝试延迟滚动视图的触摸,甚至完全取消它...
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
}
}
......什么也没有。
答案 0 :(得分:0)
我明白了。 我在viewDidLoad中添加了shouldRequireFailureOfGestureRecognizer。它需要在它之外。
为了将它放在外面并使其工作,你需要在viewDidLoad之外创建你的手势变量(在我的情况下" longPressedGesture")作为选项,如:
var longPressedGesture : UILongPressGestureRecognizer?
然后在viewDidLoad中创建手势
longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(PhotosViewController.Scrubber(_:)))
longPressedGesture.delegate = self
self.scrubberCollectionView.addGestureRecognizer(longPressedGesture)