很简单。我按照Apple文档中的说明设置了sprite的centerRect,但是显示的图像变形了(因为我没有定义centerRect属性)。我的代码:
let sprite = SKSpriteNode()
sprite.texture = SKTexture(imageNamed: "ImageName")
sprite.centerRect = CGRect(x: 0.49, y: 0.49, width: 0.02, height: 0.02)
sprite.scale(to:CGSize(width: myCustomWidth, height: myCustomHeight))
//sprite.size = CGSize(width: myCustomWidth, height: myCustomHeight)
我不知道我在哪里犯了错误,或者我的代码中是否缺少某些错误。
This is how it looks
This is what I want
先感谢您。
最佳答案
视宽度和高度而定,对方应变形。中央部分仅是图像的2%* 2%,是缩放操作期间的主要缩放部分。
您可以成像四个角不会改变,因此如果比例缩放到原始图像的(2X * 2X),即从(0.02 * 0.02-> 1.02 * 1.02)缩放到2500倍,则中心部分可能会变形很大。在图像的中心。
您的代码没有问题。
到目前为止,这个概念是正确的。如果您无法获得所需的图像,则可能是原始图像尺寸的大小。
sprite.texture = SKTexture(imageNamed: "ImageName")
print (sprite.texture?.size()) // If size is very large here, then you cannot get what you want. The size of image should be small than target. Actually only when you zoom in the texture, i.e, the current size is smaller than CGSize(width: myCustomWidth, height: myCustomHeight), you may get the result.
sprite.centerRect = CGRect(x: 0.49, y: 0.49, width: 0.02, height: 0.02)
sprite.scale(to:CGSize(width: myCustomWidth, height: myCustomHeight))
sprite.size = CGSize(width: myCustomWidth, height: myCustomHeight)
最后一部分是我的测试代码。
class TestViewController: UIViewController{
@IBOutlet weak var skview: SKView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let sprite1 = SKSpriteNode()
sprite1.texture = SKTexture(imageNamed: "round.png")
print (sprite1.texture?.size())
sprite1.centerRect = CGRect(x: 0.49, y: 0.49, width: 0.02, height: 0.02)
print (sprite1.size)
sprite1.scale(to:CGSize(width: 300, height:100))
sprite1.size = CGSize(width: 300, height: 100)
print (sprite1)
skview.scene?.addChild(sprite1)
}}
关于ios - 无法使SKSpriteNode的centerRect属性起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52798877/