我有一个整数类型的变量,当我想向左滑动时我想将变量增加一。相反,用一个向右滑动的姿势将一个变量减去一个。
问题是当我向左滑动时,switch语句不识别并执行代码,例如当变量为1时。它只在我回过头来执行它。为什么switch语句也不识别向左滑动语句。我目前只是输出到控制台。
class ViewController: UIViewController {
//counter varaible
var counter:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
println(counter)
//swipe left
let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipedLeft:")
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
view.addGestureRecognizer(swipeLeft)
//swipe right
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipedRight:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
view.addGestureRecognizer(swipeRight)
}
//swiped Left
func swipedLeft(didLeft:UISwipeGestureRecognizer) {
counter += 1
println(counter)
}
//swiped right
func swipedRight(didRight:UISwipeGestureRecognizer) {
counter -= 1
println(counter)
switch counter {
case 1:
println("one")
case 2:
println("two")
default:
println("bye")
}
}