iOS 系統(tǒng)的CoreGraphic框架可以簡單的實(shí)現(xiàn)粒子系統(tǒng)的效果:
實(shí)現(xiàn)粒子系統(tǒng)主要用到兩個類CAEmitterLayer和CAEmitterCell
下面是例子印蔬,雪花飄落的效果
//添加背景圖
???UIImage*bgImage = [UIImageimageNamed:@"背景.jpg"];
???self.view.backgroundColor= [UIColor???colorWithPatternImage:bgImage];
???//粒子圖層
???CAEmitterLayer*snowLayer = [CAEmitterLayerlayer];
??? snowLayer.backgroundColor= [UIColorredColor].CGColor;
???//發(fā)射位置
??? snowLayer.emitterPosition=CGPointMake(0,0);
???//發(fā)射源的尺寸
??? snowLayer.emitterSize=CGSizeMake(640,1);
???//發(fā)射源的形狀
??? snowLayer.emitterMode=kCAEmitterLayerSurface;
???//發(fā)射模式
??? snowLayer.emitterShape=kCAEmitterLayerLine;
???//存放粒子種類的數(shù)組
???NSMutableArray*snow_array =@[].mutableCopy;
???for(NSIntegeri=1; i<4; i++) {
???????//snow
???????CAEmitterCell*snowCell = [CAEmitterCellemitterCell];
??????? snowCell.name=@"snow";
???????//產(chǎn)生頻率
??????? snowCell.birthRate=15.0f;
???????//生命周期
??????? snowCell.lifetime=30.0f;
???????//運(yùn)動速度
??????? snowCell.velocity=1.0f;
???????//運(yùn)動速度的浮動值
??????? snowCell.velocityRange=10;
???????//y方向的加速度
??????? snowCell.yAcceleration=2;
???????//拋灑角度的浮動值
??????? snowCell.emissionRange=0.5*M_PI;
???????//自旋轉(zhuǎn)角度范圍
??????? snowCell.spinRange=0.25*M_PI;
???????//粒子透明度在生命周期內(nèi)的改變速度
??????? snowCell.alphaSpeed=2.0f;
???????//cell的內(nèi)容,一般是圖片
???????NSString*snow_str = [NSStringstringWithFormat:@"snow%ld",i];
??????? snowCell.contents= (id)[UIImageimageNamed:[NSStringstringWithFormat:@"%@.png",snow_str
???????????????????????????????????????????????????? ]].CGImage;
??????? [snow_arrayaddObject:snowCell];
??? }
???//添加到當(dāng)前的layer上
??? snowLayer.shadowColor= [[UIColorredColor]CGColor];
??? snowLayer.cornerRadius=1.0f;
??? snowLayer.shadowOffset=CGSizeMake(1,1);
??? snowLayer.emitterCells= [NSArrayarrayWithArray:snow_array];
??? [self.view.layerinsertSublayer:snowLayeratIndex:0];