Cover by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

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:

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required