粒子動畫中的關鍵概念是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];
新聞熱點
疑難解答