20.9. 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:

CGContextSetShadow procedure

This 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). Specifying 0.0f will 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.

CGContextSetShadowWithColor procedure

This procedure accepts the exact same parameters as CGContextSetShadow, with one addition. This fourth parameter, of type CGColorRef, sets the color ...

Get iOS 7 Programming Cookbook 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.