如何让我的 UISwipeGestureRecognizer 工作?

How do I make my UISwipeGestureRecognizer work?

我有一个 UIGestureRecognizer,我想在用户向右和向下滑动时识别它,以及 printl "swiped right" 和 "swiped down"。我已经尝试了很多不同的东西,但我仍然无法让它工作。有人可以告诉我我的代码有什么问题吗?

@IBAction func respondToSwipeGesture(sender: UISwipeGestureRecognizer) {

        if let swipeGesture = sender as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
                println("Swiped right")
            case UISwipeGestureRecognizerDirection.Down:
                println("Swiped down")
            default:
                break
            }
        }
    }

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)

尝试

 //do not need IBAction here
 func respondToSwipeGesture(sender: UISwipeGestureRecognizer) {
    switch sender.direction {//Do not need to optional cast here
    case UISwipeGestureRecognizerDirection.Right:
        println("Swiped right")
    case UISwipeGestureRecognizerDirection.Down:
        println("Swiped down")
    default:
        break
    }
}

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)
    self.view.userInteractionEnabled = true //Here need to be true
}

它在我的模拟器上运行良好