粒子发送器:CAEmitterLayer
粒子:CAEmitterCell
渐变图层:CAGradientLayer
添加代码:
@interface RootViewController () @property (nonatomic, strong) CAEmitterLayer *emitterLayer; @end @implementation RootViewController -(CAEmitterLayer *)emitterLayer{ if (_emitterLayer) { return _emitterLayer; } _emitterLayer = [[CAEmitterLayer alloc]init]; [self.view.layer addSublayer:_emitterLayer]; return _emitterLayer; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; self.title = @"大头儿子"; UIImageView *View = [[UIImageView alloc]initWithFrame:self.view.frame]; // View.center = CGPointMake(200,450); View.contentMode = UIViewContentModeScaleAspectFit; View.image = [UIImage imageNamed:@"1.jpg"]; [self.view addSubview:View]; self.emitterLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, 600); // self.view.backgroundColor = [UIColor blackColor]; // 设置粒子发送器 每秒钟发送的数量 self.emitterLayer.birthRate = 1; // self.emitterLayer.emitterPosition 中心点 self.emitterLayer.position = CGPointMake(400,900); self.emitterLayer.emitterShape = kCAEmitterLayerCircle; // 设置粒子发送器的样式 self.emitterLayer.renderMode = kCAEmitterLayerOldestLast; self.emitterLayer.emitterMode = kCAEmitterLayerVolume; NSMutableArray *array = [[NSMutableArray alloc]init]; for (int i=0; i<5; i++) { CAEmitterCell *cell3 = [CAEmitterCell emitterCell]; cell3.contents = (id)[UIImage imageNamed:[NSString stringWithFormat:@"桃心%d",i+1]].CGImage; // 粒子出生量 cell3.birthRate = 1; // 存活时间->单位秒 cell3.lifetime = 5; cell3.lifetimeRange = 2; // 发送速度 cell3.velocity = 50; cell3.velocityRange = 100; // 粒子发送的方向 cell3.emissionLatitude = 50*(i+1)*M_PI/180; // 发送的加速度 cell3.yAcceleration = -50; // 散发粒子的范围 - 弧度 cell3.emissionRange = 200; // 把粒子的cell 放到粒子发送器上 [array addObject:cell3]; } self.emitterLayer.emitterCells = array; //创建渐变图层对象 CAGradientLayer *layer = [CAGradientLayer layer]; layer.frame = self.view.frame; //图层的透明度 layer.opacity = 0.2; //设置图层颜色渐变的开始点 layer.startPoint = CGPointMake(0, 0); //设置图层颜色渐变的结束点 layer.endPoint = CGPointMake(1,0.5); //颜色渐变的百分比 数组 layer.locations = @[@0.2,@0.6]; //渐变颜色的数组 数组不接受的CGColor类型 强转成id类型的 layer.colors = @[(id)[UIColor redColor].CGColor,(id)[UIColor whiteColor].CGColor,(id)[UIColor cyanColor].CGColor,]; [self.view.layer addSublayer:layer]; // }
效果图: