我以前見過類似的問題,但似乎沒有提供明確的解決方案。目前我有一個SKScene
與孩子,包含一個SKView
。注意:我沒有使用遊戲項目;相反,我在正常的UIView
中使用SKView
,並且只需添加SKView
作爲UIView
的子視圖。精靈本身是一個小圖像,它只佔據整個幀的中下部分的一定部分。代碼看起來是這樣的:SpriteKit:在調整大小/縮放比例時不需要的精靈拉伸
let imageView = SKView()
imageView.frame = CGRect(x: ..., y: ..., width: ..., height: ...) //all are predefined constants
let sprite = SKSpriteNode(texture: SKTexture(imageNamed: "image"))
sprite.position = CGPoint(x: imageView.frame.width * 0.5, y: imageView.frame.height * 0.5) // so it's positioned in the center
let scene = SKScene(size: imageView.frame.size) // also tried with sprite.size, but not working
scene.addChild(sprite)
scene.scaleMode = .aspectFit
imageView.presentScene(scene)
self.frame.addSubview(imageView)
現在的問題是精靈是不是在它的原始形狀;它沿着垂直軸延伸。我試圖用所有5個選項來更改scaleMode
:.fill
/.aspectFill
/.aspectFit
/.resizeFill
/none
,但它們都沒有給出精靈的原始形狀/比例。我也嘗試改變imageView
的幀的常量,但是隻削減精靈(如果imageView.frame.height
減少)。所以我可以知道如何解決這個問題?
我已經將你的代碼輸入到GameViewController的viewDidLoad標準遊戲模板中,並且我沒有看到用'.aspectFit'伸展... –