Swift version: 5.4
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 Join a FREE crash course for iOS devs who want to become complete senior developers — from October 18th to 24th. Learn how to apply iOS app architecture patterns through a series of lectures and practical coding sessions.
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.