Graphics Reference
In-Depth Information
NSInteger
x = floor(bounds.origin.x / layer.tileSize.width * scale);
NSInteger
y =
floor
(bounds.origin.y / layer.tileSize.height * scale);
Correcting the scale in this way also means that our snowman image will be rendered at
half the size on Retina devices (at a total size of only 1024×1024 points instead of
2048×2048 as before). This usually doesn't matter for the types of images normally
displayed with
CATiledLayer
(such photographs and maps, which are designed to be
zoomed and viewed at various scales), but it's worth bearing in mind.
In iOS 5, Apple introduced a new
CALayer
subclass called
CAEmitterLayer
.
CAEmitterLayer
is a high-performance particle engine designed to let you create real-
time particle animations such as smoke, fire, rain, and so on.
CAEmitterLayer
acts as a container for a collection of
CAEmitterCell
instances
that define a particle effect. You will create one or more
CAEmitterCell
objects as
templates
for the different particle types, and the
CAEmitterLayer
is responsible for
instantiating a stream of particles based on these templates.
A
CAEmitterCell
is similar to a
CALayer
: It has a
contents
property that can be
set using a
CGImage
, as well as dozens of configurable properties that control the
appearance and behavior of the particles. We won't attempt to describe each and every
property in detail, but they are well documented in the
CAEmitterCell
class header file.
Let's try an example: We'll create a fiery explosion effect by emitting particles in a circle
with varying velocity and alpha. Listing 6.13 contains the code for generating the
explosion. You can see the result in Figure 6.13.
Listing 6.13
Creating an Explosion Effect with
CAEmitterLayer
#import
"ViewController.h"
#import
<QuartzCore/QuartzCore.h>
@interface
ViewController
()
@property
(
nonatomic
,
weak
)
IBOutlet
UIView
*containerView;
@end
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];