skip to content

In Core Animation, why do technique cards with layer shadows stutter while scrolling, and when do shadowPath and shouldRasterize fix it or make it worse?

level: seniorimportance: should knowfreq 45%

answer

  1. the shadow's shape comes from somewhere
  2. the composited alpha channel
  3. hand the outline over up front
  4. a cached bitmap at scale 1
  5. redrawn whenever its content changes

basics

~20 s

With shadowPath nil, a layer's shadow is built from its composited alpha, so the content must render before the shadow, frame after frame. Setting shadowPath supplies the outline; shouldRasterize caches a bitmap that helps unchanging content and hurts changing content.

solid answer

~50 s

A shadow needs an outline. With `shadowPath` left `nil`, the layer takes it from its composited alpha channel, the shape of everything it and its sublayers draw, so that content has to be rendered first, in a separate pass, before the shadow can be blurred and composited; while a list scrolls, that repeats for every visible card. Assigning `shadowPath`, for example `UIBezierPath(roundedRect:cornerRadius:).cgPath` recomputed in `layoutSubviews()` because it does not follow bounds changes, gives the outline directly; Apple's documentation says an explicit path usually improves rendering performance. `shouldRasterize` renders the layer, shadow and filters included, into a bitmap the renderer may reuse between frames. That pays off when the content is expensive and static, and costs more when the content changes, because the bitmap must be redrawn. `rasterizationScale` defaults to `1`, so set it to the display scale or the card looks soft.

code

swift · 26 lines
swift
import UIKit

final class TechniqueCard: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.cornerRadius = 16
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.2
        layer.shadowRadius = 8
        layer.shadowOffset = CGSize(width: 0, height: 4)
    }

    required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }

    override func layoutSubviews() {
        super.layoutSubviews()
        // Give the shadow its outline instead of deriving it from the rendered alpha.
        layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: layer.cornerRadius).cgPath
    }

    func cacheAsBitmap(_ enabled: Bool) {
        layer.shouldRasterize = enabled
        // The default rasterizationScale is 1.0, which looks soft on a 2x or 3x screen.
        layer.rasterizationScale = traitCollection.displayScale
    }
}

go deeper

for a junior

Recall that a layer shadow without a shadowPath is expensive and that setting the path to the card's outline is the usual fix.

for a middle

Explain why an alpha-derived shadow needs the content rendered first, and why the path must be recomputed on layout.

for a senior

Judge when rasterizing helps and when it hurts, set its scale correctly, and back each change with a measurement on a real device.

for a principal

Set the team default for shadows in shared components, explicit paths and no blanket rasterization, so list performance does not depend on each screen's author.

## Where a shadow's shape comes from A `CALayer` draws a shadow when `shadowOpacity` is above zero, shaped by `shadowColor`, `shadowRadius` (the blur) and `shadowOffset`. What the properties do not say is **which outline** casts the shadow. There are two sources: - **`shadowPath` set:** the shadow is the given `CGPath`, filled with the non-zero winding rule. The path is copied on assignment. - **`shadowPath` nil**, the default: the shadow is derived from the layer's **composited alpha channel**, the combined shape of its background, contents, border and every sublayer. The second source is convenient, because it follows any shape automatically, and it is the expensive one. Two defaults also surprise people: `shadowOpacity` defaults to `0`, so nothing draws until you raise it, and `shadowOffset` defaults to `(0, -3)`, which with iOS's top-left origin puts the shadow slightly above the layer unless you set it. ## The cost of an alpha-derived shadow To know the alpha of a card whose shape comes from a rounded background, an icon and a text label, Core Animation must first render that content, then blur its alpha into the shadow, then composite both. That separate rendering is the offscreen pass people refer to when profiling shadows. It happens every frame the shadowed content needs drawing again, and in a scrolling list of technique cards with drop shadows it multiplies by the number of cards on screen. The symptom is a list that drops frames on older devices while each card on its own looks fine. ## shadowPath done right Apple's documentation says that specifying an explicit path usually improves rendering performance, and that sharing one path reference across layers helps too. The details that trip people up: - The path is in the layer's coordinate space and **does not follow `bounds`**. Set it in `layoutSubviews()` (or `layoutSublayers()` for a standalone layer) so it is recomputed after every size change. - **`shadowPath` has no implicit animation**, like every `CGPath` layer property. When the card's bounds animate, animate `shadowPath` with an explicit `CABasicAnimation` of the same duration, or the shadow snaps to its new shape. - The path must match the visible outline. A rectangular path under a rounded card shows square shadow corners. - A path extending outside the bounds is not clipped unless the normal masking rules apply, so `masksToBounds` still removes it. ## shouldRasterize: a bitmap cache with conditions **`shouldRasterize`** (default `false`) renders the layer and its sublayers into a bitmap in the layer's local space, then composites that bitmap. Shadows and filters are baked in; the layer's current `opacity` is applied afterwards. The renderer *may* keep and reuse the bitmap from frame to frame, which is the whole benefit. | Situation | Effect of `shouldRasterize = true` | |---|---| | Complex, static card with shadow and filters, moving as a whole | can help: the bitmap is reused while the card moves | | Card whose countdown text or ring redraws every frame | hurts: the bitmap is re-rendered on top of the normal work | | Card scaled up by a transform during a zoom | looks soft: the cached bitmap is stretched with the magnification filter | | `rasterizationScale` left at the default of `1` on a 3x screen | looks blurry: the bitmap has a third of the needed resolution | Set `rasterizationScale` from the view's `traitCollection.displayScale`; `UIScreen.main` is deprecated since iOS 26. Every rasterized layer also holds a bitmap in memory, so rasterizing a whole list is not free. ## Diagnosing a stuttering card list 1. Reproduce on the oldest supported device and profile with Instruments rather than guessing. 2. Look for shadows without `shadowPath`; adding the path is the cheapest, safest fix. 3. Look for `mask` layers and filters on cells. A mask layer's alpha channel decides which content shows, and filters can force the layer to be rasterized before compositing. 4. Try `shouldRasterize` only on content that stays unchanged while it moves, set the scale, and measure again; remove it if the numbers do not improve. 5. Re-measure after each single change on the same device and scroll path, so one fix's effect is not hidden by another's. Frame-time budgets and how to track dropped frames as a metric are covered with mobile performance design; the question here is what a single layer asks the renderer to do.

  • Why does a shadowPath set once in init look wrong after the card resizes?
    The path is copied on assignment in the layer's coordinate space and never follows `bounds`. After a rotation or a size change it still describes the old size. Recompute it in `layoutSubviews()`, and because `shadowPath` has no implicit animation, animate it explicitly when the bounds animate.
  • When does shouldRasterize make a scrolling card list worse?
    When the rasterized content changes, because each change re-renders the bitmap on top of the normal drawing, and when the card is scaled up, because the cached bitmap is stretched instead of re-rendered. Each rasterized layer also costs bitmap memory, so caching every cell rarely pays.

  • A layer shadow's cost depends only on its blur radius, not its outline.
  • shadowPath updates by itself whenever the layer's bounds change.
  • shouldRasterize always speeds up a layer that has a shadow.
  • rasterizationScale matches the screen's scale by default.
  • shouldRasterize bakes the layer's current opacity into the cached bitmap.