Adding a gradient with Core Graphics

Now we'll replace the solid orange color with a gradient, but this time, a gradient that faces the other way:

  1. Delete the following lines of code from the drawMinutes method:
 darkColor.setFill()  path.fill() 

This time, instead of initiating an NSGradient as we did for the clock face, we will draw into the graphics context, using CGGradient. The Core Graphics methods that we use to do that work a little differently to AppKit's methods, as we'll see shortly.

To begin with, we need to specify three values with which to configure the CGGradient object. We'll go through these one at a time.

Firstly, we need to define a CGColorSpace, which will inform Core Graphics that we are using RGB colors.

  1. Add the following ...

Get Mastering macOS Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.