Introductory resources for Metal, Apple’s low level graphics API, are typically geared towards 3D games and scenes. In this post, I’ll translate Apple’s Using Metal to Draw a View’s Contents MetalKit sample from Objective-C to Swift, seeking to balance changing as little as possible with a ‘Swift-y’ approach.
The first three lines are trivial enough. But where to put them? Many of Apple’s samples simple opt for trusty ‘viewDidLoad’. Let’s instead override UIKit’s ‘loadView’ function:
We’ll keep strong references to the MTKView and our renderer in the ViewController: