粒子动画中的关键概念是CAEmitterLayer和 CAEmitterCell,其中CAEmitterLayer是发射源,CAEmitterCell是发射出去的粒子.
其余的一些属性官网或者百度都可以知道.
另附代码如下
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 , self.view.bounds.size.height, 200, 200)]; view.backgroundColor = [UIColor redColor]; view.layer.cornerRadius = 50; [self.view addSubview:view]; //create particle emmitter layer //CAEmitterLayer负责发射粒子(粒子也可以发射粒子) CAEmitterLayer *emitter = [CAEmitterLayer layer]; emitter.frame = view.bounds; //决定粒子发射形状的中心点 emitter.emitterPosition = CGPointMake(100, 100); emitter.emitterZPosition = 3.0; emitter.emitterShape = kCAEmitterLayerPoints;//发射器形状 emitter.emitterMode = kCAEmitterLayerPoints;//3d圆形的体积内发射 emitter.PReservesDepth = YES; //是否开启三维空间模式 emitter.emitterDepth = 2.0;//发射器的深度,有时可能会产生立体效果 emitter.spin = 5;//粒子的旋转角度 [view.layer addSublayer:emitter]; emitter.renderMode = kCAEmitterLayerAdditive;//渲染模式 //emitter.emitterPosition = CGPointMake(emitter.frame.size.width /2.0, emitter.frame.size.height / 2.0); //creat a particle template] CAEmitterCell *cell = [[CAEmitterCell alloc] init]; cell.contents = (__bridge id _Nullable)([UIImage imageNamed:@"d00"].CGImage); cell.birthRate = 15.0; cell.lifetime = 5.0; cell.enabled = YES;//是否打开粒子的渲染效果 // cell.color = [UIColor colorWithRed:1 green:0.5 blue:0.1 alpha:1.0].CGColor; cell.alphaspeed = -0.4; cell.velocity = 50; //粒子的速度 cell.velocityRange = 70;//粒子的速度范围 cell.emissionRange = M_PI * 2.0; //creat a particle template] CAEmitterCell *cell2 = [[CAEmitterCell alloc] init]; cell2.contents = (__bridge id _Nullable)([UIImage imageNamed:@"d01"].CGImage); cell2.birthRate = 15.0; cell2.lifetime = 5.0; cell2.color = [UIColor colorWithRed:1 green:0.5 blue:0.1 alpha:1.0].CGColor; cell2.alphaSpeed = -0.4; cell2.velocity = 50; cell2.velocityRange = 50; cell2.emissionRange = M_PI * 2.0; //add pareticle template to emitter emitter.emitterCells = @[cell,cell2];
新闻热点
疑难解答