Elevation
public struct Elevation : Equatable
Encapsulates design shadows and applies them to a layer.
This data structure is designed to closely match how shadows are exported from Figma and also how they are specified for web/CSS. cf. https://www.w3.org/TR/css-backgrounds-3/#box-shadow
-
Specifies the horizontal offset of the shadow.
A positive value draws a shadow that is offset to the right of the box, a negative length to the left.
Declaration
Swift
public let xOffset: CGFloat
-
Specifies the vertical offset of the shadow.
A positive value offsets the shadow down, a negative one up.
Declaration
Swift
public let yOffset: CGFloat
-
Specifies the blur radius.
Negative values are invalid. If the blur value is zero, the shadow’s edge is sharp. Otherwise, the larger the value, the more the shadow’s edge is blurred
Declaration
Swift
public let blur: CGFloat
-
Specifies the spread distance.
Positive values cause the shadow to expand in all directions by the specified radius. Negative values cause the shadow to contract.
Declaration
Swift
public let spread: CGFloat
-
Specifies the color of the shadow.
This value should be opaque (i.e. alpha channel = 1.0 or 0xFF).
opacity
is a separate property.Declaration
Swift
public let color: UIColor
-
Specifies the opacity of the shadow.
Possible values range from
0.0
to1.0
(inclusive).0
means no shadow, and1
means fully opaque.Declaration
Swift
public let opacity: Float
-
Whether
apply(layer:cornerRadius:)
should set the shadowPath. Defaults totrue
.Should be
false
for layers that are not a simple rect or round-rect (e.g. ellipse, circle, donut, or other irregular shape such as a logo). Note that whenfalse
thespread
property is ignored. Image rendering performance will be better when set totrue
(avoids an off-screen render pass).Declaration
Swift
public let useShadowPath: Bool
-
Initializes an elevation
Declaration
Swift
public init( xOffset: CGFloat, yOffset: CGFloat, blur: CGFloat, spread: CGFloat, color: UIColor, opacity: Float, useShadowPath: Bool = true )
Parameters
xOffset
the horizontal offset of the shadow
yOffset
the vertical offset of the shadow
blur
the blur radius
spread
the spread distance
color
the color of the shadow
opacity
the opacity of the shadow
useShadowPath
whether to set the shadowPath. Default is
true
-
Computes how far from the edges of the view the shadow will extend in each direction.
This is determined by offset, blur, and spread, but not by opacity (unless zero). The rectangle enclosing the shadow should be the view’s frame outset by
extent
.Declaration
Swift
public var extent: UIEdgeInsets { get }
-
Applies elevation to a layer.
In most cases
cornerRadius
should matchlayer.cornerRadius
. Note: This method does not setlayer.cornerRadius
, only the layer’s shadow properties.Declaration
Swift
public func apply(layer: CALayer, cornerRadius: CGFloat? = nil)
Parameters
layer
the layer to apply the elevation to.
cornerRadius
the corner radius to use for the shadow path. Pass
nil
to uselayer.cornerRadius
(the default). Ignored whenuseShadowPath == false