在SpriteKit游戏中将UIView添加到场景的后面

我试图在swift2中制作一个带有雪影响的降临节日历。 我在使用SpiteKit时使用游戏模板。

这是我到目前为止的代码:

GameScene.swift

import SpriteKit class GameScene: SKScene { /*override func touchesBegan(touches: Set, withEvent event: UIEvent?) { if let touch = touches.first { let location = touch.locationInNode(self) print(location) } }*/ func test() { //Generate Doors //Initilization var adventDoors = [AdventDoor]() let offset = CGVector(dx: 10,dy: 10) var size = CGRectMake(offset.dx, offset.dy, 60, 60) var ypos:CGFloat = 20 var xpos:CGFloat = 10 var index = 0 var xDoor = AdventDoor(frame: size) let randomIdentifier = [Int](1...24).shuffle() for _ in 1...4 { for i in 1...6 { size = CGRectMake(xpos, ypos, 60, 60) xDoor = AdventDoor(frame: size) xDoor.opaque = false xDoor.restorationIdentifier = "\(randomIdentifier[index])" xDoor.generateDoor() adventDoors.append(xDoor) print("1...6") ypos += 80 //xpos += 20 index++ if i == 6 { print("Moving to next view") } } xpos += 80 ypos = 20 } size = CGRectMake(10, 500, 300, 60) xDoor = AdventDoor(frame: size) xDoor.opaque = false xDoor.restorationIdentifier = "\(25)" xDoor.generateDoor() adventDoors.append(xDoor) index = 0 for door in adventDoors { index++ self.view?.addSubview(door) } print("\(index) doors were added") } } 

GameViewController.swift

 class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() if let scene = GameScene(fileNamed:"GameScene") { // Configure the view. let skView = self.view as! SKView //skView.showsFPS = true //skView.showsNodeCount = true /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true /* Set the scale mode to scale to fit the window */ scene.scaleMode = .AspectFill scene.backgroundColor = UIColor.greenColor() skView.presentScene(scene) scene.test() //Snow let wrappedSnowPath = NSBundle.mainBundle().pathForResource("Snow", ofType: "sks") if let snowPath = wrappedSnowPath { let snowEmitter:SKEmitterNode = NSKeyedUnarchiver.unarchiveObjectWithFile(snowPath) as! SKEmitterNode let screenBounds = UIScreen.mainScreen().bounds snowEmitter.position = CGPointMake(screenBounds.width, screenBounds.height) scene.addChild(snowEmitter) } } } override func shouldAutorotate() -> Bool { return true } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { if UIDevice.currentDevice().userInterfaceIdiom == .Phone { return .AllButUpsideDown } else { return .All } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Release any cached data, images, etc that aren't in use. } override func prefersStatusBarHidden() -> Bool { return true } } 

AdventDoor.swift只包含一个自定义UIView(AdventDoor)以及更多function

这就是它的样子。

在此处输入图像描述

如您所见,雪SKEmitterNode粒子位于AdventDoors后面而不是前面。

如何让我的雪在UIView Advent Doors门前展示而不是落后?

您需要做的是重新排列您的视图布局,而不是为门添加子视图。 您需要的是UIView作为主视图,然后将SKView作为子视图添加到主视图中。 然后,如果你想在场景创建过程中添加门,你需要做self.view.superview.insertSubView(door, atIndex:0)self.view.superview.insertSubView(door, belowSubView:self.view)这样门就放在了场景子视图的后面