Objective-c particle animation, objective-c Particle

Source: Internet
Author: User

Objective-c particle animation, objective-c Particle

I have posted a few articles on SpriteKit. the animations involved are material-type image switching or conventional animation effects, and do not involve the particle animation to be mentioned today, today, particle animation is a required class for better and cool effects in games. In OC, particle animation mainly involves two classes: CAEmitterLayer and CAEmitterCell. The first class is the animation execution class, the second is the animation implementation class. Although there are only two classes with few attributes, it is difficult to understand these two classes and use them flexibly. It should be because particle animation is the most difficult technology in game development. Currently, most of the popular game development frameworks use tools to make particle animation effects, such as 3dmax, although oc optimizes the class for particle animation easily, it is still difficult to understand. The next step is to explain the two classes first, and then paste a case that has been explained in detail for each sentence of code.

1. involved categories

1. CAEmitterLayer (particle animation execution class. Here you think of it as the director class for implementing particle animation)
/* The array of emitter cells attached to the layer. Each object must
* Have the CAEmitterCell class .*/
This array is the additional transmitter layer of this layer. Each object must have at least one particle class,
@ Property (nullable, copy) NSArray <CAEmitterCell *> * emitterCells;

2. CAEmitterCell (particle class, which can be thought of as a particle animation implementation class. After it is used to implement particle animation, it is placed in the CAEmitterLayer for execution)

 

Ii. Detailed Cases

1 @ interface SnowViewController () 2 3 @ end 4 5 @ implementation SnowViewController 6 7-(void) viewDidLoad 8 {9 [super viewDidLoad]; 10 11 flakeLayer = [CAEmitterLayer layer]; 12 13 14 CGRect bounds = [[UIScreen mainScreen] bounds]; 15 16 // set the position of the transmitter on the screen, 17 flakeLayer. emitterPosition = CGPointMake (bounds. size. width/2, bounds. size. height/3); 18 19/* The following is the size of the transmitter, which determines the birth position range of the particle. For example, if the value below is 0 or 0, the starting point of all particle birth trajectories will be the center point of the transmitter, you can think of it as the center point of the simulator. You can combine this range with the emitterShape attribute to think */20 flakeLayer. emitterSize = CGSizeMake (); 21 22 // The emission model determines the shape 23 flakeLayer of the Motion Track of the born particle. emitterShape = kCAEmitterLayerLine; // kCAEmitterLayerRectangle24 25 // launch mode 26 flakeLayer. emitterMode = kCAEmitterLayerSurface; // kCAEmitterLayerOutline27 28 29 // declare a particle class 30 CAEmitterCell * flakeCell = [CAEmitterCell emitterCell]; 31 // is a CGImageRef object, the image to be presented by the particle 32 flakeCell. contents = (id) [[UIImage imageNamed: @ "FFFlake.png"] CGImage]; 33 // number of particles generated per second 34 flakeCell. birthRate = 100; 35 // 36 flakeCell disappears after 1.2 seconds of life cycle. lifetime = 120.0; 37 // life cycle range: 38 flakeCell. lifetimeRange = 0.5; 39 // speed // the speed of the particle from birth to the end point of the trajectory. If it is set to large, the number of particles on the screen increases, the birth rate will increase by 40 flakeCell. velocity = 100; 41 // speed range: 42 flakeCell. velocityRange = 10; 43 // The acceleration component in the y direction of the particle -- moving 44 flakeCell to the y axis of the particle. xAcceleration = 10; 45 // the direction of the upx-y plane...> emissionLatitude: angle of the Z axis of the launch 46 flakeCell. emissionlongpolling =-M_PI/2; 47 // random emission angle of the transmitter in a range. If it is set to 0, the angle of the transmitter is fixed. If it is set to the circumference rate, then, the emission range of the born particle will randomly emit the particle angle 48 flakeCell in the range of 360 degrees. emissionRange = M_PI; 49 50 // here it indicates the effect of the completed motion track after the particle is born. If it is 0, it will be moved smoothly in the life cycle, if we set it to 51 // 360, it will rotate in the form of 52 flakeCell In the lifecycle. spinRange = 0; 53 54 55 flakeCell. scale = 1.0; // scaling ratio: 56 flakeCell. scaleSpeed = 0.1; // 1.0; // The scaling speed is 57 flakeCell. scaleRange = 1.0; // scaling ratio; 58 59 flakeCell. color = [[UIColor colorWithRed: 0.6 green: 0.6 blue: 0.6 alpha: 1.0] CGColor]; // the color of the particle 60 flakeCell. redRange = 1.0; // the color red of a particle can change the range; 61 flakeCell. redSpeed = 0.1; // The change speed of the particle red in the lifecycle; 62 flakeCell. blueRange = 1.0; // the color of a particle blue can change in the range of 63 flakeCell. blueSpeed = 0.1; // The change speed of the particle blue in the lifecycle 64 flakeCell. greenRange = 1.0; // the color of the particles green can change the range; 65 flakeCell. greenSpeed = 0.1; // The change speed of the particle green in the lifecycle; 66 flakeCell. alphaSpeed =-0.08; // The change speed of particle transparency during the lifecycle 67 68 flakeLayer. emitterCells = [NSArray arrayWithObject: flakeCell]; // particle emission 69 70 [self. view. layer insertSublayer: flakeLayer atIndex: 0]; 71 72}

 

Related Article

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: info-contact@alibabacloud.com 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.