问题描述
是的,对于Swift来说仍然很新,但是在我当前的(第一个)应用程序中,我开始以像素为单位定位和调整SKSpriteNodes的大小。现在突然之间,我不得不切换到要点,因为一切都炸了。
我已经缩小了应用safeAreaLayount约束的原因,我的GameViewControler代码在本文的底部。
我的问题实际上是两个问题:
1-有没有办法让我正确地告诉Swift使用点而不是像素(反之亦然)?
2-是否有理由在另一个上使用?
import UIKit
import SpriteKit
import GameplayKit
var originalS : UIView!
var myGlobalVars = GlobalVars(backGround: SKSpriteNode(),pegHolder: SKSpriteNode(),widthPoints: 0.0,heightPoints: 0.0,widthPixels: 0.0,heightPixels: 0.0,passGo: false,sceneRect: .zero)
class GameViewController: UIViewController {
let logo = UIImage(named: "startup")
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView?
{
myGlobalVars.widthPixels = UIScreen.main.nativeBounds.width
myGlobalVars.heightPixels = UIScreen.main.nativeBounds.height
myGlobalVars.widthPoints = UIScreen.main.bounds.width
myGlobalVars.heightPoints = UIScreen.main.bounds.height
let imageView = UIView()
originalS = imageView
originalS.backgroundColor = .clear
view.addSubview(originalS)
///////comment out this section and Swift uses pixels//////
originalS.translatesAutoresizingMaskIntoConstraints = false
originalS.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
originalS.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,constant: 0).isActive = true
originalS.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
originalS.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,constant: -0).isActive = true
///////comment out the section above and Swift uses pixels//////
var scene : GameScene!
DispatchQueue.main.async {
scene = GameScene(size: CGSize(width: originalS.frame.width,height: originalS.frame.height))
scene.anchorPoint = CGPoint(x: 0.5,y: 0.5)
scene.backgroundColor = .black
scene.scaleMode = .aspectFit
myGlobalVars.sceneRect = scene.frame
view.presentScene(scene)
}
myGlobalVars.passGo = true
//change these at end of development
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override var prefersStatusBarHidden: Bool {
return false
}
}
解决方法
this是我认为的类似问题
如此:
-
有没有一种方法可以正确地告诉Swift使用点而不是像素(反之亦然)? 答:所有这些转换都是自动进行的,因为我们有1x,2x,3x像素的屏幕尺寸,而且系统知道应显示哪个图像。因此,系统管理不同的图像,并且您始终在处理点。 (系统知道她何时在2倍屏幕设备或3倍屏幕设备上使用,以及应该使用哪个图像。
-
是否有理由使用一个? 答:您只需要一直使用积分。 (以及所有屏幕类型1x,2x,3x图像的提供程序图像。例如,您的图像块为100X100。您需要在项目中添加3张图像-100X100 200X200 300X300,系统会根据屏幕类型放置所需的图像