Graphics Reference
In-Depth Information
shadowPath
We mentioned the shadowPath property in Chapter 2. If your layer is a simple geometric
shape like a rectangle or rounded rectangle (which it will be if it doesn't contain any
transparent parts or sublayers), it is easy to create a shadow path that matches its shape and
this will greatly simplify the calculations that Core Animation has to do to draw the
shadow, avoiding the need to precompose the layer offscreen. This makes a huge difference
to the performance.
If your layer has a more complex shape, it might be awkward to generate the correct
shadow path, in which case you may want to consider pregenerating your shadow as a
background image using a paint program.
Blending and Overdraw
As mentioned in Chapter 12, there is a limit to the number of pixels that the GPU can draw
each frame (known as the fill rate ), and while it can comfortably draw an entire screen full
of pixels, it might begin to lag if it needs to keep repainting the same area multiple times
due to overlapping layers ( overdraw ).
The GPU will discard pixels in layers that are fully obscured by another layer, but
calculating whether a layer is obscured can be complicated and processor intensive.
Merging together the colors from several overlapping translucent pixels ( blending ) is also
expensive. You can help to speed up the process by ensuring that layers do not make use of
transparency unless they need to. Whenever possible, you should do the following:
Set the backgroundColor of your view to a fixed, opaque color.
Set the opaque property of the view to YES .
This reduces blending (because the compositor knows that nothing behind the layer will
contribute to the eventual pixel color) and speeds up the calculations for avoiding overdraw
because Core Animation can discard any completely obscured layers in their entirety
instead of having to test each overlapping pixel individually.
If you are using images, try to avoid alpha transparency unless it is strictly needed. If the
image will appear in front of a fixed background color, or a static background image that
doesn't need to move relative to the foreground, you can prefill the image background and
avoid runtime blending.
If you are using text, a UILabel with a white background (or any other solid color) is more
efficient to draw than one with a transparent background.
Finally, by judicious use of the shouldRasterize property, you can collapse a static layer
hierarchy into a single image that doesn't need to be recomposited each frame, avoiding
any performance penalties due to blending and overdraw between the sublayers.
 
Search WWH ::




Custom Search