标签:
首先我们先导入系统的库:
添加下面的代码即可:
#import "FireworksViewController.h" @interface FireworksViewController () @end @implementation FireworksViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; self.title = @"烟花动画"; //设置重底部发射cells CAEmitterLayer *fireworksEmitter = [CAEmitterLayer layer]; CGRect viewBounds = self.view.layer.bounds; //发射位置 fireworksEmitter.emitterPosition = CGPointMake(viewBounds.size.width / 2.0, viewBounds.size.height - 200); //发射源大小 fireworksEmitter.emitterSize = CGSizeMake(viewBounds.size.width / 2.0, 0.0); //发射模式 fireworksEmitter.emitterMode = kCAEmitterLayerOutline; //发射形状 fireworksEmitter.emitterShape = kCAEmitterLayerLine; //渲染模式(kCAEmitterLayerAdditive与kCAEmitterLayerUnordered相比较, 合并重叠的部分使看上去更亮); fireworksEmitter.renderMode = kCAEmitterLayerAdditive; //初始化随机产生的种子 fireworksEmitter.seed = (arc4random()%100) + 1; // Create the rocket CAEmitterCell *rocket = [CAEmitterCell emitterCell]; //设置每秒产生的粒子数 rocket.birthRate = 2.0; //周围发射角度 rocket.emissionRange = 0.0 * M_PI; //速度 rocket.velocity = 350; //速度范围 rocket.velocityRange = 200; //粒子的y方向的加速度分量 rocket.yAcceleration = 75; //设置生命周期,即在屏幕上要显示多长时间 rocket.lifetime = 1.02; rocket.contents = (id)[[UIImage imageNamed:@"DazRing"] CGImage]; //粒子尺寸 rocket.scale = 0.2; //粒子颜色 rocket.color = [[UIColor redColor]CGColor]; //粒子的颜色green,red,blue 能改变的范围 rocket.greenRange = 1.0; rocket.redRange = 1.0; rocket.blueRange = 1.0; //子旋转角度范围 rocket.spinRange = M_PI; // the burst object cannot be seen, but will spawn the sparks // we change the color here, since the sparks inherit its value CAEmitterCell* burst = [CAEmitterCell emitterCell]; burst.birthRate = 1.0; // at the end of travel burst.velocity = 0; burst.scale = 2.5; //粒子red,blue,green 在生命周期内的改变速度 burst.redSpeed =-1.5; // shifting burst.blueSpeed =+1.5; // shifting burst.greenSpeed =+1.0; // shifting burst.lifetime = 0.35; // and finally, the sparks CAEmitterCell* spark = [CAEmitterCell emitterCell]; spark.birthRate = 400; spark.velocity = 120; spark.emissionRange = 2* M_PI; // 360 deg spark.yAcceleration = 75; // gravity spark.lifetime = 2; spark.contents = (id) [[UIImage imageNamed:@"桃心"] CGImage]; //缩放比例速度 spark.scaleSpeed =-0.2; spark.greenSpeed =-0.1; spark.redSpeed = 0.4; spark.blueSpeed =-0.1; spark.alphaSpeed =-0.25; //子旋转角度 spark.spin = 2* M_PI; //子旋转角度范围 spark.spinRange = 2* M_PI; // putting it together fireworksEmitter.emitterCells = [NSArray arrayWithObject:rocket]; rocket.emitterCells = [NSArray arrayWithObject:burst]; burst.emitterCells = [NSArray arrayWithObject:spark]; [self.view.layer addSublayer:fireworksEmitter]; //设置背景图片 UIImage *image = [UIImage imageNamed:@"1.jpg"]; //__bridge id self.view.layer.contents = (__bridge id)(image.CGImage); } @end
效果:
标签:
原文地址:http://www.cnblogs.com/LzwBlog/p/5724829.html