Graphics Reference
In-Depth Information
Another common feature in iOS is the
drop shadow.
Drop shadows are cast behind a view
to imply depth. They are used to indicate layering and priority (such as when a modal alert
is presented in front of another view), but they are also sometimes used for purely cosmetic
purposes (to give controls a more solid appearance).
A drop shadow can be shown behind any layer by setting the
shadowOpacity
property
to a value greater than zero (the default). The
shadowOpacity
is a floating-point value
and should be set between 0.0 (invisible) and 1.0 (fully opaque). Setting a value of 1.0 will
show a black shadow with a slight blur and a position slightly above the layer. To tweak the
appearance of the shadow, you can use a trio of additional
CALayer
properties:
shadowColor
,
shadowOffset
, and
shadowRadius
.
The
shadowColor
property, as its name implies, controls the shadow color and is a
CGColorRef
, just like the
borderColor
and
backgroundColor
properties. The
default shadow color is black, which is probably what you want most of the time anyway
(colored shadows occur rarely in real life, and can look a bit strange).
The
shadowOffset
property controls the direction and distance to which the shadow
extends. The
shadowOffset
is a
CGSize
value, with the width controlling the shadow's
horizontal offset and the height controlling its vertical offset. The default
shadowOffset
is {0, -3}, which results in a shadow positioned 3 points above the layer along the Y axis.
Why does the default shadow point upward? Although Core Animation was adapted from
Layer Kit (the private animation framework created for iOS), its first appearance as a public
framework was on Mac OS, which uses an inverted coordinate system with respect to iOS
(the Y axis points upward). On a Mac, the same default
shadowOffset
value results in a
downward
-pointing shadow, so the default direction makes more sense in that context (see
Figure 4.5).
Figure 4.5
The default
shadowOffset
appearance on iOS (left) and Mac OS (right)