ios 如何让 UIScrollView 滚动并拥有手势识别器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23841045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to have a UIScrollView scroll and have a gesture recognizer?
提问by Phillip
I have a gesture recognizer on a UIScrollView, however it hardly ever gets called as the UIScrollView eats all the gestures.
我在 UIScrollView 上有一个手势识别器,但是它几乎不会被调用,因为 UIScrollView 吃掉了所有的手势。
I partially got around this issue with this line: [scrollView.panGestureRecognizer requireGestureRecognizerToFail:rightSwipe];
however, this line results in my recognizer always being accepted (the desired behavior) and the scroll view not scrolling.
我通过这一行部分解决了这个问题:[scrollView.panGestureRecognizer requireGestureRecognizerToFail:rightSwipe];
然而,这一行导致我的识别器总是被接受(所需的行为)并且滚动视图不滚动。
That is, when you scroll, the recognizer is accepted but the view doesn't scroll.
也就是说,当您滚动时,识别器被接受,但视图不会滚动。
How can I get around this, or is there an alternate solution?
我该如何解决这个问题,或者是否有其他解决方案?
Thanks!
谢谢!
采纳答案by Warif Akhand Rishi
Make a subclass of UIScrollView
. Add this method in your new subclass
创建 的子类UIScrollView
。在您的新子类中添加此方法
- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Make your scrollView class to your new scrollview subclass.
将您的 scrollView 类设置为新的 scrollview 子类。
回答by abe973t
The way that works for me is subclass UIScrollView
and conform to the UIGestureRecognizerDelegate
in that subclass. Then call this method.
对我有用的方式是子类UIScrollView
并符合UIGestureRecognizerDelegate
该子类中的 。然后调用这个方法。
class ATScrollView: UIScrollView, UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
回答by Mert Kahraman
Don't forget to assign proper delegation to your gesture recognizer mechanism:
不要忘记为您的手势识别器机制分配适当的委托:
So, for example:
因此,例如:
Set a .up
direction gesture recognizer
设置.up
方向手势识别器
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeUp.direction = .up
swipeUp.delegate = self // set delegate
self.addGestureRecognizer(swipeUp)
Set a .down
direction gesture recognizer
设置.down
方向手势识别器
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeDown.direction = .down
swipeDown.delegate = self // set delegate
self.addGestureRecognizer(swipeDown)
Also don't forget to conform to delegation:
也不要忘记遵守委托:
YourViewController: UIGestureRecognizerDelegate
Set simultaneous recognition:
设置同时识别:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
Then move on to your logic...
然后继续你的逻辑......
Hope it helps!
希望能帮助到你!
回答by Esqarrouth
Swift method,
Need to add UIGestureRecognizerDelegate to your class:
Swift 方法,
需要将 UIGestureRecognizerDelegate 添加到您的类中:
class ViewController: UIViewController, UIGestureRecognizerDelegate {
Need to set its delegate to self:
需要将其委托设置为 self:
override func viewDidLoad() {
super.viewDidLoad()
var scrollView = UIScrollView(frame: self.view.frame)
scrollView.delegate = self
}
Add this part in your class methods to active simultaneous gestures:
将此部分添加到您的类方法中以激活同步手势:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
回答by biomiker
Forgive me for this tangent but I'm posting this answer just in case anyone else makes the same misdiagnosis I did. If like me the above solutions aren't working for you, make sure you're not trying to attach your UISwipeGestureRecognizer to more than one view. It can only work with one view, and that will be view to which it was most recently attached!
原谅我这个切线,但我发布这个答案是为了以防其他人做出同样的误诊。如果像我一样上述解决方案对您不起作用,请确保您没有尝试将 UISwipeGestureRecognizer 附加到多个视图。它只能与一个视图一起使用,并且该视图将是它最近附加到的视图!