Swift version: 5.1
SKShapeNode class is a fast and convenient way to draw arbitrary shapes in your games, including circles, lines, rounded rectangles and more. You can assign a fill color, a stroke color and width, plus other drawing options such as whether it should glow – yes, really.
The example code below draws a rounded rectangle smack in the middle of the game scene, giving it a red fill color and a 10-point blue border:
let shape = SKShapeNode() shape.path = UIBezierPath(roundedRect: CGRect(x: -128, y: -128, width: 256, height: 256), cornerRadius: 64).cgPath shape.position = CGPoint(x: frame.midX, y: frame.midY) shape.fillColor = UIColor.red shape.strokeColor = UIColor.blue shape.lineWidth = 10 addChild(shape)
SAVE 20% ON iOS CONF SG The largest iOS conference in Southeast Asia is back in Singapore for the 5th time in January 2020, now with two days of workshops plus two days of talks on SwiftUI, Combine, GraphQL, and more! Save a massive 20% on your tickets by clicking on this link.
Available from iOS 7.0 – see Hacking with Swift tutorial 17
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.