我需要使用滑动来识别向下和向右滑动的手势。但是在快速的UISwipeGestureRecognizer上已经确定了正确的方向。而且我不知道如何将此方向用于其他方向。
UISwipeGestureRecognizer每个方向都需要一个。有点奇怪,因为该UISwipeGestureRecognizer.direction属性是选项样式的位掩码,但是每个识别器只能处理一个方向。您可以根据需要将它们全部发送到同一个处理程序,并在那里进行排序,或者将它们发送到不同的处理程序。这是一个实现:
UISwipeGestureRecognizer
UISwipeGestureRecognizer.direction
override func viewDidLoad() { super.viewDidLoad() let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture)) swipeRight.direction = .right self.view.addGestureRecognizer(swipeRight) let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture)) swipeDown.direction = .down self.view.addGestureRecognizer(swipeDown) } @objc func respondToSwipeGesture(gesture: UIGestureRecognizer) { if let swipeGesture = gesture as? UISwipeGestureRecognizer { switch swipeGesture.direction { case .right: print("Swiped right") case .down: print("Swiped down") case .left: print("Swiped left") case .up: print("Swiped up") default: break } } }
斯威夫特3:
override func viewDidLoad() { super.viewDidLoad() let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) swipeRight.direction = UISwipeGestureRecognizerDirection.right self.view.addGestureRecognizer(swipeRight) let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) swipeDown.direction = UISwipeGestureRecognizerDirection.down self.view.addGestureRecognizer(swipeDown) } func respondToSwipeGesture(gesture: UIGestureRecognizer) { if let swipeGesture = gesture as? UISwipeGestureRecognizer { switch swipeGesture.direction { case UISwipeGestureRecognizerDirection.right: print("Swiped right") case UISwipeGestureRecognizerDirection.down: print("Swiped down") case UISwipeGestureRecognizerDirection.left: print("Swiped left") case UISwipeGestureRecognizerDirection.up: print("Swiped up") default: break } } }