Caemitterlayer to achieve particle effects

Source: Internet
Author: User


In iOS 5, Apple introduced a new CALayer subclass called CAEmitterLayer . CAEmitterLayeris 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.

CAEmitterLayerLooks like a lot ofCAEmitterCellThe container, theseCAEmitierCellDefines an example effect. You will define one or more for different example effectsCAEmitterCellAs a template, at the same timeCAEmitterLayerResponsible for instantiating a particle stream based on those templates. OneCAEmitterCellSimilar to aCALayer: It has acontentsproperty 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 canCAEmitterCellFound 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.

CAEmitterLayerThe property itself controls the location and shape of the entire example system. Some properties such asbirthRatelifetimeAndcelocity, these properties areCAEmitterCellThere 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
  • renderModeThat 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.