如何识别所有4个方向的滑动

我需要使用滑动来识别滑动手势,然后右键。 但在迅速的UISwipeGestureRecognizer已经预先确定了正确的方向..我不知道如何使这个使用其他方向..

您需要为每个方向都有一个UISwipeGestureRecognizer 。 这UISwipeGestureRecognizer.direction因为UISwipeGestureRecognizer.direction属性是一个选项样式的位掩码,但是每个识别器只能处理一个方向。 你可以把它们全部发送给同一个处理程序,然后把它们整理出来,或者发送给不同的处理程序。 这里有一个实现:

 override func viewDidLoad() { super.viewDidLoad() var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:") swipeRight.direction = UISwipeGestureRecognizerDirection.Right self.view.addGestureRecognizer(swipeRight) var swipeDown = UISwipeGestureRecognizer(target: self, action: "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 } } } 

Swift 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 } } } 

我只是觉得自己做出了贡献,最终看起来更优雅:

 func addSwipe() { let directions: [UISwipeGestureRecognizerDirection] = [.Right, .Left, .Up, .Down] for direction in directions { let gesture = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipe:")) gesture.direction = direction self.addGestureRecognizer(gesture) } } func handleSwipe(sender: UISwipeGestureRecognizer) { print(sender.direction) } 

从故事板:

  1. 添加四个滑动手势识别器到您的视图。
  2. 使用属性检查器中的目标方向设置每个对象。 您可以select右,左,上或下
  3. 一个接一个,select滑动手势识别器,控制+拖动到你的视图控制器。 插入名称(让我们说leftGesture,rightGesture,upGesture和downGesture),将连接更改为:操作并键入到:UISwipeGestureRecognizer

从你的viewController:

 @IBAction func rightGesture(sender: UISwipeGestureRecognizer) { print ("Right") } @IBAction func leftGesture(sender: UISwipeGestureRecognizer) { print ("Left") } @IBAction func upGesture(sender: UISwipeGestureRecognizer) { print = ("Up") } @IBAction func downGesture(sender: UISwipeGestureRecognizer) { print ("Down") } 

看起来事情最近已经改变了。 在XCode 7.2中,以下方法工作:

 override func viewDidLoad() { super.viewDidLoad() let swipeGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipe:") swipeGesture.direction = [.Down, .Up] self.view.addGestureRecognizer(swipeGesture) } func handleSwipe(sender: UISwipeGestureRecognizer) { print(sender.direction) } 

在iOS 8.4和9.2的模拟器上以及9.2上的实际设备上进行testing。

或者, 在这里使用mlcollard的方便扩展:

 let swipeGesture = UISwipeGestureRecognizer() { print("Gesture recognized !") } swipeGesture.direction = [.Down, .Up] self.view.addGestureRecognizer(swipeGesture) 

Apple Swift 3.1版 – Xcode版本8.3(8E162)

从亚历山大卡萨涅的方法方便的方式

 let directions: [UISwipeGestureRecognizerDirection] = [.up, .down, .right, .left] for direction in directions { let gesture = UISwipeGestureRecognizer(target: self, action: #selector(YourClassName.handleSwipe(gesture:))) gesture.direction = direction self.view?.addGestureRecognizer(gesture) } func handleSwipe(gesture: UISwipeGestureRecognizer) { print(gesture.direction) switch gesture.direction { case UISwipeGestureRecognizerDirection.down: print("down swipe") case UISwipeGestureRecognizerDirection.up: print("up swipe") case UISwipeGestureRecognizerDirection.left: print("left swipe") case UISwipeGestureRecognizerDirection.right: print("right swipe") default: print("other swipe") } } 

UISwipeGestureRecognizerdirection属性具有以下定义:

 var direction: UISwipeGestureRecognizerDirection 

此手势识别器的滑动的允许方向。


Swift 3.0.1(及以下)的问题是,即使UISwipeGestureRecognizerDirection符合OptionSet ,下面的代码片段也会被编译,但不会产生任何积极的预期结果:

 // This compiles but does not work let gesture = UISwipeGestureRecognizer(target: self, action: #selector(gestureHandler)) gesture.direction = [.right, .left, .up, .down] self.addGestureRecognizer(gesture) 

作为解决方法,您将不得不为每个所需的direction创build一个UISwipeGestureRecognizer


下面的Playground代码显示了如何使用Array的map方法为同一个UIView和相同的selector实现多个UISwipeGestureRecognizer

 import UIKit import PlaygroundSupport class SwipeableView: UIView { convenience init() { self.init(frame: CGRect(x: 100, y: 100, width: 100, height: 100)) backgroundColor = .red [UISwipeGestureRecognizerDirection.right, .left, .up, .down].map({ let gesture = UISwipeGestureRecognizer(target: self, action: #selector(gestureHandler)) gesture.direction = $0 self.addGestureRecognizer(gesture) }) } func gestureHandler(sender: UISwipeGestureRecognizer) { switch sender.direction { case [.left]: frame.origin.x -= 10 case [.right]: frame.origin.x += 10 case [.up]: frame.origin.y -= 10 case [.down]: frame.origin.y += 10 default: break } } } class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white view.addSubview(SwipeableView()) } } let controller = ViewController() PlaygroundPage.current.liveView = controller 

它可以通过简单地声明一个函数来处理你所有的滑动UISwipeGestureRecognizer的方向,这里是我的代码

  let swipeGestureRight = UISwipeGestureRecognizer(target: self, action:#selector(ViewController.respondToSwipeGesture(_:)) ) swipeGestureRight.direction = UISwipeGestureRecognizerDirection.right self.view .addGestureRecognizer(swipeGestureRight) let swipeGestureLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:))) swipeGestureLeft.direction = UISwipeGestureRecognizerDirection.left self.view.addGestureRecognizer(swipeGestureLeft) let swipeGestureUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:))) swipeGestureUp.direction = UISwipeGestureRecognizerDirection.up self.view.addGestureRecognizer(swipeGestureUp) let swipeGestureDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:))) swipeGestureDown.direction = UISwipeGestureRecognizerDirection.down self.view.addGestureRecognizer(swipeGestureDown) 

这里是涉及swipedirectionfunction的function

func respondToSwipeGesture(_ sender:UIGestureRecognizer){

 if let swipeGesture = sender as? UISwipeGestureRecognizer { switch swipeGesture.direction { case UISwipeGestureRecognizerDirection.right: print("right swipe") case UISwipeGestureRecognizerDirection.left: print("leftSwipe") case UISwipeGestureRecognizerDirection.up: print("upSwipe") case UISwipeGestureRecognizerDirection.down: print("downSwipe") default: break } } 

}

Nate的答案只是一个更酷的语法:

 [UISwipeGestureRecognizerDirection.right, UISwipeGestureRecognizerDirection.left, UISwipeGestureRecognizerDirection.up, UISwipeGestureRecognizerDirection.down].forEach({ direction in let swipe = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) swipe.direction = direction self.view.addGestureRecognizer(swipe) }) 

使用起来很简单,请按照下面的代码进行操作,享受…👇🏻

  //SwipeGestureMethodUsing func SwipeGestureMethodUsing () { //AddSwipeGesture [UISwipeGestureRecognizerDirection.right, UISwipeGestureRecognizerDirection.left, UISwipeGestureRecognizerDirection.up, UISwipeGestureRecognizerDirection.down].forEach({ direction in let swipe = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) swipe.direction = direction window?.addGestureRecognizer(swipe) }) } //respondToSwipeGesture 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 } } } 

快乐编码😊