Swift version: 5.2
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)
SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.
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.
Link copied to your pasteboard.