我试图让UISwipeGestureRecognizer在Swift 3中工作,默认滑动右边工作正常但不向上或向左。
我通过控制拖动Action来尝试它
@IBAction func hole(_ recognizer: UISwipeGestureRecognizer) { if (recognizer.direction == UISwipeGestureRecognizerDirection.left) { print("left") }else if recognizer.direction == .right { print("right") }else { print("other") } }
而且,在ViewDidLoad中
//gesture recognisers let swipeRight = UISwipeGestureRecognizer(target: self,action: "holeSwiped:") swipeRight.direction = UISwipeGestureRecognizerDirection.right self.view.addGestureRecognizer(swipeRight) let swipeLeft = UISwipeGestureRecognizer(target: self,action: "holeSwiped:") swipeLeft.direction = UISwipeGestureRecognizerDirection.left self.view.addGestureRecognizer(swipeLeft)
我的方法
func holeSwiped(gesture: UISwipeGestureRecognizer) { if let swipeGesture = gesture as? UISwipeGestureRecognizer{ switch swipeGesture.direction { case UISwipeGestureRecognizerDirection.right: print("right swipe") case UISwipeGestureRecognizerDirection.left: print("left swipe") default: print("other swipe") } } }
现在,除默认权限外,没有任何滑动工作正在进行。有任何想法吗?
第1步:在viewDidLoad()方法中添加滑动手势。
override func viewDidLoad() { super.viewDidLoad() let swipeLeft = UISwipeGestureRecognizer(target: self,action: #selector(handleGesture)) swipeLeft.direction = .left self.view.addGestureRecognizer(swipeLeft) let swipeRight = UISwipeGestureRecognizer(target: self,action: #selector(handleGesture)) swipeRight.direction = .right self.view.addGestureRecognizer(swipeRight) let swipeUp = UISwipeGestureRecognizer(target: self,action: #selector(handleGesture)) swipeUp.direction = .up self.view.addGestureRecognizer(swipeUp) let swipeDown = UISwipeGestureRecognizer(target: self,action: #selector(handleGesture)) swipeDown.direction = .down self.view.addGestureRecognizer(swipeDown) }
第2步:检查handleGesture()方法中的手势检测
func handleGesture(gesture: UISwipeGestureRecognizer) -> Void { if gesture.direction == UISwipeGestureRecognizerDirection.right { print("Swipe Right") } else if gesture.direction == UISwipeGestureRecognizerDirection.left { print("Swipe Left") } else if gesture.direction == UISwipeGestureRecognizerDirection.up { print("Swipe Up") } else if gesture.direction == UISwipeGestureRecognizerDirection.down { print("Swipe Down") } }
我希望这会对某人有所帮助。