我正在尝试向我的视图添加滑动手势识别器。这是我的主视图控制器文件的viewDidLoad()
方法中的代码片段:
self.view.addGestureRecognizer(UISwipeGestureRecognizer(target: self, action: #selector(self.swipeHandler(_:))))
self.view.isUserInteractionEnabled = true
这是我在viewDidLoad()
函数下面编写的处理程序方法:
@IBAction func swipeHandler(_ gestureRecognizer : UISwipeGestureRecognizer) {
print("called")
if gestureRecognizer.state == .ended {
// Perform action.
print("ended")
}
}
当我滑动时,无论方向是什么,我都看不到控制台上打印的任何内容。
我认为问题出在addGestureRecognizerMethod()
,因为当我将其更改为敲击识别器(相同的行,只是UITapGestureRecognizer而不是UISwipeGestureRecognizer)时,我在控制台中打印了“被叫”和“结束”。
任何提示?
gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
像这样:
扩展名YourViewController:UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true }