我试图为我的节点树确定正确的缩放因子,使其完全适合我的演示矩形,所以我试图在我的所有节点周围找到最小的边界矩形。 Apple的文档说,calculateAccumulatedFrame"计算父坐标系中包含节点及其所有后代内容的矩形。"这听起来像我需要的,但它没有给我我期望的紧密配合。我完整的游乐场代码是:
import SpriteKit
import PlaygroundSupport
let view:SKView = SKView(frame: CGRect(x: 0, y: 0, width: 800, height: 800))
PlaygroundPage.current.liveView = view
let scene:SKScene = SKScene(size: CGSize(width: 1000, height: 800))
scene.scaleMode = SKSceneScaleMode.aspectFill
view.presentScene(scene)
let yellowBox = SKSpriteNode(color: .yellow, size:CGSize(width: 300, height: 300))
yellowBox.position = CGPoint(x: 400, y: 500)
yellowBox.zRotation = CGFloat.pi / 10
scene.addChild(yellowBox)
let greenCircle = SKShapeNode(circleOfRadius: 100)
greenCircle.fillColor = .green
greenCircle.position = CGPoint(x: 300, y: 50)
greenCircle.frame
yellowBox.addChild(greenCircle)
let uberFrame = yellowBox.calculateAccumulatedFrame()
let blueBox = SKShapeNode(rect: uberFrame)
blueBox.strokeColor = .blue
blueBox.lineWidth = 2
scene.addChild(blueBox)
蓝色矩形的左下边缘看起来不错,但为什么蓝色矩形和顶部和右边的绿色圆圈之间有间隙?