从 SKScene 到 UIViewcontroller 的过渡 [英] Transition from SKScene to UIViewcontroller

查看:22
本文介绍了从 SKScene 到 UIViewcontroller 的过渡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有:

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 {
...
}

我知道如何从 SKScene 过渡到 SKScene,但我需要从 SKScene 过渡到 UIViewcontroller.

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

推荐答案

首先,使用视图控制器的协议为自己设置一个委托.

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

protocol UIViewControllerDelegate{
}

见这里:https://makeapppie.com/2014/07/01/swift-swift-using-segues-and-delegates-in-navigation-controllers-part-1-the-template/ 关于如何做到这一点的好教程

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

创建一个将托管此委托的 SKView 类

Create an SKView class that will be hosting this delegate

class GameView : SKView
{
       var delegate : UIViewControllerDelegate?
}

然后在 UIViewController 类中的 viewDidLoad 上,将委托分配给您的视图控制器.

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

例如

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.

在您的场景中,您只需将场景的视图投射到 GameView,并使用委托进行过渡

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()
       }
  }
}

但是请注意,从场景转换到视图控制器是不可能的,因为它们是 2 种不同的动物.您将转换视图,因此无法使用为视图提供的动画.

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.

这篇关于从 SKScene 到 UIViewcontroller 的过渡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆