我试图向从主场景继承的场景添加滑动手势(GameScene)
extension GameScene {
class Scene1: GameScene {
override func didMove(to view: SKView) {
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
view.addGestureRecognizer(swipeDown)
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeUp.direction = UISwipeGestureRecognizerDirection.up
view.addGestureRecognizer(swipeUp)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
view.addGestureRecognizer(swipeLeft)
}
}
class Scene2: GameScene {
override func didMove(to view: SKView) {
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
view.addGestureRecognizer(swipeDown)
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeUp.direction = UISwipeGestureRecognizerDirection.up
view.addGestureRecognizer(swipeUp)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
view.addGestureRecognizer(swipeLeft)
}
}
}
每次我必须在所有场景中再次编写此代码。也许,还有另外一条出路。
答案 0 :(得分:0)
是的,如果您阅读了代码,您将看到您从未向场景添加手势,而是将其添加到视图中。现在问问自己,你有什么其他对象可以访问视图?答案是视图控制器。这是您要放置手势代码的位置。