如何在 SKScene 中呈现 UIViewController?

Posted

技术标签:

【中文标题】如何在 SKScene 中呈现 UIViewController?【英文标题】:How to present a UIViewController in a SKScene? 【发布时间】:2015-07-02 15:18:02 【问题描述】:

我一直在试图弄清楚如何在 SKScene 中呈现 UIViewController。

更具体地说,我正在处理 GameScene.swift,当按下图像时,我想展示 uiviewcontroller。

import SpriteKit

class GameScene: SKScene 



    override func didMoveToView(view: SKView) 
        /* Setup your scene here */


        backgroundColor = SKColor.blackColor()
        var timeBetweenDots = NSTimer.scheduledTimerWithTimeInterval(0.35, target: self, selector: Selector("spawnWhiteDots"), userInfo: nil, repeats: true)
    



    override func update(currentTime: CFTimeInterval) 
        /* Called before each frame is rendered */

    




    func spawnWhiteDots()

    var dotWhite = SKSpriteNode(imageNamed: "whiteDot.png")

    dotWhite.name = "destroyWhiteDot"

    var randomDotPlacement = arc4random() % 9
    switch (randomDotPlacement)
    case 1:
        dotWhite.position = CGPointMake(self.size.width / 2.92, self.size.height / 1)
        break
    case 2:
        dotWhite.position = CGPointMake(self.size.width / 1.81, self.size.height / 1)
        break
    case 3:
        dotWhite.position = CGPointMake(self.size.width / 2.24, self.size.height / 1)
        break
    case 4:
        dotWhite.position = CGPointMake(self.size.width / 1.52, self.size.height / 1)
        break
    case 5:
        dotWhite.position = CGPointMake(self.size.width / 2.92, self.size.height / 1)
        break
    case 6:
        dotWhite.position = CGPointMake(self.size.width / 2.24, self.size.height / 1)
        break
    case 7:
        dotWhite.position = CGPointMake(self.size.width / 1.52, self.size.height / 1)
        break
    case 8:
        dotWhite.position = CGPointMake(self.size.width / 1.81, self.size.height / 1)
        break
    default:
        break
    

    let fallAction = SKAction.moveToY(-50, duration: 3.0)

    dotWhite.runAction(SKAction.repeatActionForever(fallAction))

    addChild(dotWhite)

    


    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) 
        /* Called when a touch begins */
        for touch: AnyObject in touches 
            let location = (touch as UITouch).locationInNode(self)
            if let gameOverDot = self.nodeAtPoint(location).name 
                if gameOverDot == "destroyWhiteDot"
                    self.removeChildrenInArray([self.nodeAtPoint(location)])
                    ///when the image is pressed I want to present the UIViewController here the name of the viewController is gameOverScene
                
            
        
    

【问题讨论】:

How do I present a UIViewController from SKScene? 的可能重复项 【参考方案1】:

这样做的方法是创建一个 delegate 以便您可以从 GameViewController.swift 呈现视图控制器

首先在类声明之上创建一个协议

protocol GameDelegate 
    func launchViewController(#scene: SKScene)
 

现在,向 GameScene 添加一个属性来保存它

var gameDelegate: GameDelegate?

最后,从 touchesBegan 方法中调用您的方法。

gameDelegate?.launchViewController(scene: self)

ios 习惯将 self 作为参数传递给函数,这样,如果您有多个 GameScene,您可以将它们区分开来。

现在,在 GameViewController 中,添加协议实现

class GameViewController: UIViewController, GameSceneDelegate 

    func launchViewController(scene: SKScene) 
        let vc = // create view controller by storyboard or by custom initialization
        presentViewController(vc, animated: true, completion: nil)
    

    // ... OTHER STUFF ...

最后但同样重要的是,在skView.presentScene(scene)之前添加以下代码行

scene.gameDelegate = self

通过这种方式,委托允许您从另一个视图控制器呈现视图控制器

【讨论】:

【参考方案2】:

这就是我解决它的方法,因为 *** 中没有一个答案对我有用,我不得不将所有不同的部分放在一起。

首先设置GameScene.swift Touches Begin

NSNotificationCenter.defaultCenter().postNotificationName("showController", object: nil, userInfo: nil)

比在 GameViewController viewDidLoad()中

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "presentView", name: "showController", object: nil)

创建你的函数

func presentView() 
self.performSegueWithIdentifier("toMyController", sender: self)

我的转场是模态转场。

PS:请记住,gameScene 是 GameViewController 中的一个视图。

【讨论】:

以上是关于如何在 SKScene 中呈现 UIViewController?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用社交框架呈现来自 SKscene 的 UIViewController?

斯威夫特:公共 Skscene 没有以文件名呈现?

如何从 SKView 转换到 SCNView?

从 UIViewController 呈现 SKScene - Swift

如何在 Swift Playgrounds 中使用带有纹理的 SKNode 设置 SKScene?

带有圆角矩形的 SKScene