Swift SpriteKit:在调整大小/缩放时不需要的sprite拉伸

Swift SpriteKit:在调整大小/缩放时不需要的sprite拉伸,swift,sprite-kit,skscene,Swift,Sprite Kit,Skscene,我以前见过类似的问题,但似乎没有提供明确的解决方案。目前我有一个带有孩子的SKScene,包含在SKView中。注意:我没有使用游戏项目;相反,我在一个普通的UIView中使用SKView,只需添加SKView作为UIView的子视图。精灵本身是一个小图像,它只占据整个画面中底部的一部分。代码如下所示: let imageView = SKView() imageView.frame = CGRect(x: ..., y: ..., width: ..., height: ...) //all

我以前见过类似的问题,但似乎没有提供明确的解决方案。目前我有一个带有孩子的
SKScene
,包含在
SKView
中。注意:我没有使用游戏项目;相反,我在一个普通的
UIView
中使用
SKView
,只需添加
SKView
作为
UIView
的子视图。精灵本身是一个小图像,它只占据整个画面中底部的一部分。代码如下所示:

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
/
,但没有一个选项给出精灵的原始形状/比率。我还尝试更改
imageView
帧的常量,但这只会剪切精灵(如果
imageView.frame.height
减小)。那么我可以知道如何解决这个问题吗?

您的代码写得很好,所以问题可能在别处。我对它做了一点修改,这样你就可以在操场上测试了。只需新建一个游乐场,然后复制粘贴即可。您还必须为您的精灵拖动一个图像

import SpriteKit
import PlaygroundSupport

// create the outer UIView and show it on the playground
let outerView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
PlaygroundPage.current.liveView = outerView

// create the SKView and add it to the outer view
let imageView = SKView(frame: CGRect(x: 25, y: 25, width: 250, height: 250))
outerView.addSubview(imageView)

// create the scene and present it
var scene = SKScene(size: imageView.frame.size)
imageView.presentScene(scene)

// create the sprite, position it, add it to the scene
let sprite = SKSpriteNode(texture: SKTexture(imageNamed: "worf.jpg"))
sprite.position = CGPoint(x: imageView.frame.width * 0.5, y: imageView.frame.height * 0.5)
scene.scaleMode = .aspectFit
scene.addChild(sprite)
结果如下:

不要伸展!因此,您的问题可能与您放置它的UIView有关。例如,如果您添加:

outerView.transform = CGAffineTransform(scaleX: 1, y: 2)

现在,图像已按您描述的那样拉伸。查看您的包含视图及其上方的所有内容。

我已将您的代码输入到
GameViewController
viewDidLoad
标准游戏模板中,我没有看到
.aspectFit
有任何拉伸。。。