如何在SKScene中呈现UIViewController?

时间:2015-07-02 15:18:03

标签: ios swift cocoa-touch uiviewcontroller sprite-kit

我一直试图弄清楚如何在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
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

执行此操作的方法是创建委托,以便您可以从GameViewController.swift呈现View控制器

首先在类声明

上创建一个协议
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

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

答案 1 :(得分:0)

以下是我如何解决它的问题,因为stackoverflow中的所有答案都不适用于我不得不将所有部分从不同的部分中拼凑出来。

首先设置GameScene.swift触摸开始

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

我的segue是一种模态偏见。

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