15.8. Adding Shadows to Shapes
Problem
You want to be able to apply shadows to shapes that you draw on graphic contexts.
Solution
Use the CGContextSetShadow
procedure.
Discussion
It is easy to draw shadows using Core Graphics. The graphics context is the element that bears the shadow. What that means is that you need to apply the shadow to the context, draw the shapes that need the shadow, and then remove the shadow from the context (or set a new context). We will see an example of this soon.
In Core Graphics, we can use two procedures to apply a shadow to a graphics context:
CGContextSetShadowprocedureThis procedure, which creates black or gray shadows, accepts three parameters:
The graphics context on which the shadow has to be applied.
The offset, specified by a value of type
CGSize, from the right and the bottom part of each shape where the shadow has to be applied. The greater the x value of this offset is, the farther to the right of each shape the shadow will extend. The greater the y value of this offset is, the lower the shadow will extend.The blur value that has to be applied to the shadow, specified as a floating point value (
CGFloat). Specifying0.0fwill cause the shadow to be a solid shape. The higher this value goes, the more blurred the shadow will get. We will see an example of this soon.
CGContextSetShadowWithColorprocedureThis procedure accepts the exact same parameters as
CGContextSetShadow, with one addition. This fourth parameter, of typeCGColorRef, sets the color ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access