Graphics Reference
In-Depth Information
Figure 6.12
Scrolling a
CATiledLayer
using
UIScrollView
As you scroll around the image, you will notice the tiles fading in as
CATiledLayer
loads them. This is the default behavior of
CATiledLayer
. (You might have seen this
effect before in the [pre-iOS 6] Apple Maps app.) You can vary the fade duration or disable
it altogether using the
fadeDuration
property.
CATiledLayer
(unlike most UIKit and Core Animation methods) supports multi-
threaded drawing. The
-drawLayer:inContext:
method may be called concurrently
on multiple threads at the same time, so be careful to ensure that any drawing code you
implement within that method is thread-safe.
Retina Tiles
You might also have noticed that the tile images are not being displayed at Retina
resolution. To render the
CATiledLayer
at the device's native resolution, we need to set
the layer
contentsScale
to match the
UIScreen
scale, as follows:
tileLayer.
contentsScale
= [UIScreen
mainScreen
].scale;
Interestingly, the
tileSize
is specified in
pixels,
not points, so by increasing the
contentsScale
, we automatically halve the default tile size (it now equates to 128×128
points onscreen instead of 256×256). Therefore, we don't need to update the tile size
manually or supply a separate set of tiles at Retina resolution. We
will
need to adjust the tile
rendering code slightly to accommodate the change in scale, however:
//determine tile coordinate
CGRect
bounds = CGContextGetClipBoundingBox(ctx);
CGFloat
scale = [UIScreen
mainScreen
].scale;