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 You’re already busy updating your app for Swift 4.2 and iOS 12, so why not let Instabug help you find and fix bugs? Add just two lines of code to your project and receive comprehensive reports with all the feedback you need to ship a world-class app – click here to learn more!
Available from iOS 7.0 – see Hacking with Swift tutorial 17
Did this solution work for you? Please pass it on!
Other people are reading…
About the Swift Knowledge Base
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Become a Swift expert
I wrote a book called Pro Swift that's dedicated to helping you master the Swift language, and it includes seven hours of video demonstrating each technique!