gpt4 book ai didi

ios - 如何让我的 UISwipeGestureRecognizer 工作?

转载 作者:行者123 更新时间:2023-11-29 01:58:08 24 4
gpt4 key购买 nike

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

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

它在我的模拟器上运行良好 enter image description here

关于ios - 如何让我的 UISwipeGestureRecognizer 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30622403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com