SpriteKit iPad视图缩放

时间:2016-04-08 23:57:41

标签: ios xcode swift sprite-kit

我在搜索时无法找到任何相关信息,所以我决定问一下。

出于某种原因,当我尝试创建一个GameScene并尝试根据此处的建议将其扩展到iPad时。我的视图周围有黑色边框:

enter image description here

这是我的代码:

 import UIKit
 import SpriteKit

 class GameViewController: UIViewController {

   var scene: GameScene!

   override func viewDidLoad() {
     super.viewDidLoad()

    let skView = view as! SKView
    skView.multipleTouchEnabled = false;

    scene = GameScene(size: skView.bounds.size)
    scene.scaleMode = .ResizeFill
    scene.backgroundColor = UIColor.whiteColor()

    print(scene.frame.size)
    skView.presentScene(scene)
  }

  override func shouldAutorotate() -> Bool {
    return true
  }

  override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
      return .Landscape
    } else {
      return .AllButUpsideDown
    }
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
  }

  override func prefersStatusBarHidden() -> Bool {
    return true
  }
}

和此:

import SpriteKit

class GameScene: SKScene {
  override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    let testLabel = SKLabelNode(text: "Hello World!")
    testLabel.position = CGPoint(x: size.width/2, y: size.height/2)
    testLabel.fontColor = UIColor.blueColor()
    addChild(testLabel)
  }

  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */

  }

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

如何摆脱视角周围的黑色边框?

1 个答案:

答案 0 :(得分:1)

首先在部署信息部分的项目目标中设置设备到通用(我认为你用iPhone设置)看截图:

enter image description here

然后将scene.scaleMode = .ResizeFill设置为scene.scaleMode = .AspectFill,然后运行您的应用并查看没有黑色边框的结果:

enter image description here