All three of the projects in this milestone drew on frameworks outside UIKit: you tried the Multipeer Connectivity framework, Core Motion, and Core Graphics. These all form part of the wider iOS ecosystem, and I hope you’re starting to realize just how far-reaching Apple’s frameworks are – they really have done most of the work for you!
Let’s recap what you’ve learned in this milestone:
UIImagePickerController
again, and I hope at this point you’re feeling you could almost use it blindfold! That’s OK, though: we learn through repetition, so I’m not afraid to repeat things when it matters.UICollectionView
, and again you should be feeling pretty good about it by now.Codable
.categoryBitMask
, contactTestBitMask
, and collisionBitMask
in all their glory, setting up different kinds of collisions for our ball. Remember, “category” defines what something is, “collision” defines what it should be bounce off, and “contact” defines what collisions you want to be informed on.CMMotionManager
from Core Motion to read the accelerometer. Make sure you call startAccelerometerUpdates()
before you try to read the accelerometerData
property.#targetEnvironment(simulator)
code allowed us to add testing code for using the simulator, while also keeping code to read the accelerometer on devices. Xcode automatically compiles the correct code depending on your build target.And then there’s Core Graphics. Project 27 was one of the longest technique projects in all of Hacking with Swift, and with good reason: Core Graphics packed with features, and there are lots of methods and properties you need to learn to make use of it fully.
Here are some of the Core Graphics things we covered:
UIGraphicsImageRenderer
is the primary iOS graphics renderer, and can export UIImages
just by calling its image()
method and providing some drawing code.cgContext
property. This is where most of the interesting functionality is.setFillColor()
, setStrokeColor()
, setLineWidth()
and such before you do your drawing. Think of Core Graphics like a state machine: set it up as you want, do some drawing, set it up differently, do some more drawing, and so on.CGColor
rather than UIColor
. Yes, the CG is short for “Core Graphics”.translateBy()
and rotate(by:)
on a Core Graphics context to affect the way it does drawing.move(to:)
and addLine(to:)
to do custom line drawing. In combination with rotation, this created an interesting effect with very little code.UIImage
straight into a Core Graphics context by calling their draw()
method.Once you get used to Core Graphics, you start to realize that its incredible speed allows you to do procedurally generated graphics for games without too much work. More on that in project 29…
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.