Transition from SKScene to UIViewcontroller

。_饼干妹妹 提交于 2019-12-13 06:51:35

问题


For example if i have:

class SpriteKitScene: SKScene {
...
}

And in there i want to have an image, that when tapped(pressed, clicked, touched whatever) loads another file with:

class UiViewcontrollerScene: UIViewcontroller {
...
}

I know how to transition from SKScene to SKScene, but i need to transition from SKScene to UIViewcontroller.


回答1:


First, set yourself up a delegate using a protocol for your view controller.

protocol UIViewControllerDelegate{
}

See here: https://makeapppie.com/2014/07/01/swift-swift-using-segues-and-delegates-in-navigation-controllers-part-1-the-template/ for a nice tutorial on how to do that

Create an SKView class that will be hosting this delegate

class GameView : SKView
{
       var delegate : UIViewControllerDelegate?
}

Then on your viewDidLoad in your UIViewController class, assign the delegate to your view controller.

override func viewDidLoad()
{
    if let view = self.view as? GameView
    {
        view.delegate = self
    }
}

Now your view has a delegate to your view controller, From this point, in your protocol file, make a method to transition

E.G.

protocol UIViewControllerDelegate
{
    optional  func transitionToMenuVC()
}

Then apply the code to your view controller class.

class ViewController : UIViewController, UIViewControllerDelegate
{
   ...
   func transitionToMenuVC()
   {
       // do transition code here
   }
}

Now you have it all set up for your view to communicate with your view controller.

In your Scene, you would just cast the scene's view to the GameView, and use the delegate to transition

class GameScene : SKScene
{
  ...
  func transition()
  { 

       if let view = self.view as? GameView
       {
           view.delegate.transitionToMenuVC()
       }
  }
}

Do note however, it is impossible to transition from scene to view controller, because they are 2 different animals. You will be transitioning the views, and are therefor stuck using the animations provided for views.



来源:https://stackoverflow.com/questions/38310287/transition-from-skscene-to-uiviewcontroller

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