为什么这四个手势识别器中有三个有效? (向下不起作用)

时间:2014-06-19 17:11:22

标签: ios swift uiswipegesturerecognizer

这是我的班级。出于某种原因,上,左,右工作就好了。 Down似乎没有得到认可。

第一部分是viewDidLoad的一部分。我包括了整个函数" responseToSwipeGesture"

// Part of viewDidLoad
        // Setup gesture recognizers
        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)

        var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
        self.view.addGestureRecognizer(swipeLeft)

        var swipeUp = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeDown.direction = UISwipeGestureRecognizerDirection.Up
        self.view.addGestureRecognizer(swipeUp)

// viewDidLoad部分的结尾

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

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

1 个答案:

答案 0 :(得分:2)

代码中的错误

var swipeUp = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeDown.direction = UISwipeGestureRecognizerDirection.Up
self.view.addGestureRecognizer(swipeUp)

swipeDown代替swipeUp