WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

What you learned

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:

  • We covered 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.
  • We also repeated UICollectionView, and again you should be feeling pretty good about it by now.
  • You met the MultipeerConnectivity framework, which is designed for ad-hoc networking. We sent images in project 25, but any kind of data works great – yes, even data that represents custom classes you encoded using Codable.
  • We finally used 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.
  • We used CMMotionManager from Core Motion to read the accelerometer. Make sure you call startAccelerometerUpdates() before you try to read the accelerometerData property.
  • The #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:

  • The UIGraphicsImageRenderer is the primary iOS graphics renderer, and can export UIImages just by calling its image() method and providing some drawing code.
  • You can get access to the underlying Core Graphics context by reading its cgContext property. This is where most of the interesting functionality is.
  • You call 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.
  • Because Core Graphics works at a lower level than UIKit, you need to use CGColor rather than UIColor. Yes, the CG is short for “Core Graphics”.
  • You learned that you can call translateBy() and rotate(by:) on a Core Graphics context to affect the way it does drawing.
  • We tried using move(to:) and addLine(to:) to do custom line drawing. In combination with rotation, this created an interesting effect with very little code.
  • We drew attributed strings and a 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…

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.