Swift3: UISwipeGestureRecognizer in only one of putatively many SKViews

回眸只為那壹抹淺笑 提交于 2019-12-24 10:54:17

问题


Note:

Prior to asking this question I have viewed and attempted solutions posted for the following questions:

  • gesture recognizer with sprite kit and swift

  • Swift3 iOS - How to make UITapGestureRecognizer trigger function

  • how to add a gesture to skView?

  • using UIGestureRecognizer with Swift

Context:

I have implemented a basic game menu for my game following the example set here.

When I reach the GameScene I would like to have a UIGestureRecognizer that when the screen is swiped up, opens a pause menu. However prior to focusing on the implementation of the pause menu, I foremost need this UIGestureRecognizer to work.

In my GameScene, I have added a function:

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

and inside override fund didMove(to view: SKView):

    let swipeUpRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedUp(_:)))
    swipeUpRecognizer.direction = .up
    view.addGestureRecognizer(swipeUpRecognizer)

This does not work. For the first line (starts with let) I have tried several variants such as:

let swipeUpRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp"))

let swipeUpRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:"))

as well as variations to the swipedUp function, e.g.

func swipedUp(receiver: UISwipeGestureRecognizer){
    print("up")
}

Question

How to add a simple UIGestureRecognizer to only one SKView. I specify one, as due to the implementation of the menu, all the SKViews share the same GameViewController. Further, the GameScene is created only when reached via the menu, and destroyed after the game is over (so making the GameScene a global variable will not work either).

来源:https://stackoverflow.com/questions/41822340/swift3-uiswipegesturerecognizer-in-only-one-of-putatively-many-skviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!