Caemitterlayer
In iOS 5, Apple introduced a new CALayer
subclass called CAEmitterLayer
. CAEmitterLayer
is a high performance particle engine that is used to create real-time examples of animations such as Smoke, fire, rain and so on these effects.
CAEmitterLayer
Looks like a lot ofCAEmitterCell
The container, theseCAEmitierCell
Defines an example effect. You will define one or more for different example effectsCAEmitterCell
As a template, at the same timeCAEmitterLayer
Responsible for instantiating a particle stream based on those templates. OneCAEmitterCell
Similar to aCALayer
: It has acontents
property can be defined as aCGImage
, and there are some properties that can be set to control performance and behavior. We do not describe each of these attributes in detail, and you canCAEmitterCell
Found in the header file of the class.
Let's give an example. We will use particles that emit different velocities and transparency in a circle to create a fire explosion effect. Listing 6.13 contains the code that generated the explosion. Figure 6.13 is the result of the operation
Listing 6.13 using the CAEmitterLayer
create explosion effect
#import "ViewController.h" #import <QuartzCore/QuartzCore.h> @interface Viewcontroller () @property (nonatomic, Weak) Iboutlet UIView *containerview; @end @implementation viewcontroller-(void) viewdidload{[Super Viewdidload]; ? Create particle emitter layer caemitterlayer *emitter = [Caemitterlayer layer]; Emitter.frame = Self.containerView.bounds; [Self.containerView.layer Addsublayer:emitter]; Configure emitter emitter.rendermode = kcaemitterlayeradditive; Emitter.emitterposition = Cgpointmake (emitter.frame.size.width/2.0, emitter.frame.size.height/2.0); Create a particle template caemittercell *cell = [[Caemittercell alloc] init]; Cell.contents = (__bridge id) [UIImage imagenamed:@ "Spark.png"]. Cgimage; Cell.birthrate = 150; Cell.lifetime = 5.0; Cell.color = [Uicolor colorwithred:1 green:0.5 blue:0.1 alpha:1.0]. Cgcolor; Cell.alphaspeed =-0.4; Cell.velocity = 50; Cell.velocityrange = 50; Cell.emissionrange = M_PI * 2.0; Add particle template to emitter emitter.emittercells = @[cell];} @end
caemittercell
are basically divided into three types:
- The initial value of a property of this particle. For example,
example a range of changes to an attribute. such as alphaspeed
is set to-0.4, which means that the transparency of the example is reduced by 0.4 per second, so that there is an effect of gradual hours after launch.
CAEmitterLayer
The property itself controls the location and shape of the entire example system. Some properties such asbirthRate
,lifetime
Andcelocity
, these properties areCAEmitterCell
There are also. These properties are multiplied together so that you can use a value to speed up or enlarge the entire example system. Other attributes that are worth mentioning are the following:
preservesDepth
, whether to plane the example system to a layer (the default) or you can mix other layers in 3D space
renderMode
That controls how the image of a particle is blended visually. You may have noticed in the example that we set it to the kCAEmitterLayerAdditive
effect that the brightness of the overlapping part of the merged example makes it look brighter. If we set it as the default kCAEmitterLayerUnordered
, the effect is not so good.
Fig. 6.13 Effect of flame explosion
Caemitterlayer to achieve particle effects