gpt4 book ai didi

swift - 无法在 Swift playground 中向 SKScene 添加滑动识别器?

转载 作者:可可西里 更新时间:2023-11-01 02:05:57 25 4
gpt4 key购买 nike

好吧,我对 swift playground 的工作原理还是有点陌生​​,但我正在尝试将 Swift 3 中的滑动手势识别器添加到我的 swift playground。按照这个 http://www.spritekitlessons.com/gesture-recognizer-with-sprite-kit-and-swift/我现在有:

func swipedRight(sender:UISwipeGestureRecognizer){
print("swiped right")
}

func swipedLeft(sender:UISwipeGestureRecognizer){
print("swiped left")
}

func swipedUp(sender:UISwipeGestureRecognizer){
print("swiped up")
}

func swipedDown(sender:UISwipeGestureRecognizer){
print("swiped down")
}

let degree = CGFloat(M_PI_2) / 90


class GameScene: SKScene {
var selectedNode: SKNode?
var shakeAction: SKAction?

override func didMove(to view: SKView) {

/* Setup your scene here */

let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector(("swipedRight:")))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)


let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector(("swipedLeft:")))
swipeLeft.direction = .left
view.addGestureRecognizer(swipeLeft)


let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector(("swipedUp:")))
swipeUp.direction = .up
view.addGestureRecognizer(swipeUp)


let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector(("swipedDown:")))
swipeDown.direction = .down
view.addGestureRecognizer(swipeDown)
}

let frame = CGRect(x: 0, y: 0, width: 1000, height: 600) //view size
let view = SKView(frame: frame)

let scene = GameScene(size: frame.size)
view.presentScene(scene)
PlaygroundPage.current.liveView = view

这会编译,但是当我滑动时,我会收到无法识别的选择器错误,即使我确实在选择器中包含了这些功能: enter image description here

我也尝试过将函数放在类中。如何将滑动识别器添加到 Swift playground SKScene?

最佳答案

您将选择器作为字符串传递,如错误日志中所述,它们肯定有问题

尝试使用新的选择器语法 - #selector(methodName)

例子:

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

/* Swift 3 */

let swipe = UISwipeGestureRecognizer(target: self, action:#selector(handleSwipe))
view.addGestureRecognizer(swipe)

}

func handleSwipe() {
print("Swiped!")
}
}

使用字符串作为选择器已被弃用。如果 methodName() 方法不存在,则使用新的选择器语法,您将收到编译错误 - 您的应用不会因为“无法识别的选择器”而崩溃。

关于swift - 无法在 Swift playground 中向 SKScene 添加滑动识别器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42881010/

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