Returns a rectangle in the parent’s coordinate system that contains the position and size of itself and all child nodes.
SDKs
- iOS 7.0+
- macOS 10.9+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 3.0+
Framework
- Sprite
Kit
Declaration
func calculateAccumulatedFrame() -> CGRect
Discussion
The frame takes into the account the cumulative effect of the x
, y
, and z
properties of each node in the subtree.
Listing 1 shows how calculate
can be used display the bounding box of a shape node. The child node, although smaller than its parent, is rotated by 30° so that its bounds extend beyond its parent's bounds. After child
has been added to parent
, a further shape node, bounding
, is created with its size based on the accumulated frame of parentNode.
Displaying the accumulated frame of a shape node
let parentNode = SKShapeNode(rectOf: CGSize(width: 500, height: 500))
parentNode.lineWidth = 2
parentNode.strokeColor = .blue
parentNode.fillColor = .clear
let childNode = SKShapeNode(rectOf: CGSize(width: 400, height: 400))
childNode.strokeColor = .red
childNode.fillColor = .clear
childNode.zRotation = -CGFloat.pi / 6 // pi / 6 = 30°
parentNode.addChild(childNode)
let boundingBoxNode = SKShapeNode(rectOf: parentNode.calculateAccumulatedFrame().size)
boundingBoxNode.lineWidth = 1
boundingBoxNode.strokeColor = .black
boundingBoxNode.fillColor = .clear
boundingBoxNode.path = boundingBoxNode.path?.copy(dashingWithPhase: 0,
lengths: [10,10])
parentNode.addChild(boundingBoxNode)
Figure 1 shows the result of Listing 1 with parent
rendered in blue, child
rendered in red and the bounding
rendered with a dashed line.
Displaying the accumulated frame of a shape node
