@property(copy) NSArray *emitterCells; //粒子单元数组,有多少个粒子单元
@property float birthRate; 出生率因子,每秒产生的粒子数量,默认是1
@property float lifetime; 生命周期,单位是秒,默认是1
@property CGPoint emitterPosition; 粒子位置默认是CGPointZero
@property CGFloat emitterZPosition; Z轴位置默认是0
@property CGSize emitterSize; 发射尺寸
@property CGFloat emitterDepth; 发射深度依赖属性emitterShape,可以被忽略
@property(copy) NSString *emitterShape;发射形状
kCAEmitterLayerPoint 点 默认值
kCAEmitterLayerLine 线
kCAEmitterLayerRectangle 三角
kCAEmitterLayerCuboid 立方
kCAEmitterLayerCircle 圆
kCAEmitterLayerSphere 球
@property(copy) NSString *emitterMode; 发射模式
kCAEmitterLayerPoints 点群
kCAEmitterLayerOutline 轮廓
kCAEmitterLayerSurface 地面
kCAEmitterLayerVolume 团 默认值
@property(copy) NSString *renderMode;//渲染模式
kCAEmitterLayerUnordered 无序的 默认值
kCAEmitterLayerOldestFirst 从头开始
kCAEmitterLayerOldestLast 从后面开始
kCAEmitterLayerBackToFront 按照z轴
kCAEmitterLayerAdditive 加法的
@property BOOL preservesDepth; 保留深度,默认为No,如果为true会导致某些属性不确定
@property float velocity; 速度 默认为1
@property float scale; 比例,默认为1
@property float spin; 旋转角度
@property unsigned int seed; 粒子数量 默认是0
CAEmitterCell的 的类的主要属性有
+ (id)emitterCell; 构造方法
+ (id)defaultValueForKey:(NSString *)key;根据keyPath读取效果
- (BOOL)shouldArchiveValueForKey:(NSString *)key; 保存效果
@property(copy) NSString *name; 根据keyPath构造
@property(getter=isEnabled) BOOL enabled; 能否被渲染
@property float birthRate;
@property float lifetime, lifetimeRange; lifetimeRange生命周期范围,默认是0
@property CGFloat emissionLatitude, emissionLongitude; 粒子经纬度 默认是0 z轴和x轴的位置
@property CGFloat emissionRange; 发射角度,默认是0
@property CGFloat velocity, velocityRange; 速度,速度范围
@property CGFloat xAcceleration, yAcceleration, zAcceleration; 三方向加速度
@property CGFloat scale, scaleRange, scaleSpeed; 缩放,缩放范围,缩放速度
@property CGFloat spin, spinRange; 旋转角度 旋转范围
@property CGColorRef color; 粒子颜色
@property float redRange, greenRange, blueRange, alphaRange; 颜色范围和透明度范围
@property float redSpeed, greenSpeed, blueSpeed, alphaSpeed; 速度
@property(retain) id contents; 内容 类型是CGImageRef 默认是nil
@property CGRect contentsRect; 内容范围 默认是[0,0,1,1]
@property(copy) NSString *minificationFilter, *magnificationFilter; 过滤
@property float minificationFilterBias;
@property(copy) NSArray *emitterCells; 发射的子粒子
@property(copy) NSDictionary *style; 继承layer的属性
本人写的Demo
粒子层类
#ifndef IPhone5
#define IPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#endif
#import
typedef enum
{
PaticleFromePositionModeNone,
PaticleFromePositionModeTop,
PaticleFromePositionModeCenter,
PaticleFromePositionModeBottom
}
PaticleFromePositionMode;
@interface PaticleLayer : CAEmitterLayer
//发射类型
@property(nonatomic,assign) PaticleFromePositionMode fromPositonMode;
#pragma 初始化
//通过粒子图片名字/数组初始化
-(id)initWithImageName:(NSString *)imageName;
-(id)initWithImageNameArray:(NSArray*)imageArray;
-(id)init;
#pragma 粒子图片
//发射时间
-(void)setEmitterTime:(CGFloat)time;
//开始倒计时
-(void)begin;
@end
#import "PaticleLayer.h"
//粒子系统s
@interface PaticleLayer()
{
//图片数组
__strong NSArray* _imageNameArray;
//发射位置模式
PaticleFromePositionMode _fromPositonMode;
//发射时间
CGFloat _emitterTime;
}
@end
@implementation PaticleLayer
@dynamic fromPositonMode;
-(id)init
{
self = [super init];
if (self) {
_imageNameArray = [NSMutableArray array];
[self DefaultSetting];
}
return self;
}
-(id)initWithImageName:(NSString *)imageName
{
self = [super init];
if (self) {
_imageNameArray = @[imageName];
[self DefaultSetting];
}
return self;
}
-(id)initWithImageNameArray:(NSArray *)imageArray
{
self = [super init];
if (self) {
_imageNameArray = imageArray;
[self DefaultSetting];
}
return self;
}
-(void)setFromPositonMode:(PaticleFromePositionMode)fromPositonMode
{
switch (fromPositonMode) {
case PaticleFromePositionModeNone:
break;
case PaticleFromePositionModeCenter:
[self setEmitterPosition:CGPointMake(320/2.0f,(480+(IPhone5?88:0))/2)];
break;
case PaticleFromePositionModeTop:
[self setEmitterPosition:CGPointMake(320/2.0f,30)];
break;
case PaticleFromePositionModeBottom:
[self setEmitterPosition:CGPointMake(320/2.0f,(480+(IPhone5?88:0))-30)];
break;
default:
break;
}
_fromPositonMode = fromPositonMode;
}
-(PaticleFromePositionMode)fromPositonMode
{
return _fromPositonMode;
}
-(void)setEmitterTime:(CGFloat)time
{
_emitterTime = time;
}
//渐渐消失
-(void)fade
{
CABasicAnimation* fade = [CABasicAnimation animationWithKeyPath:@"opacity"];
fade.duration = 1.0;
fade.fromValue = @1.0;
fade.toValue = @0.0;
[self addAnimation:fade forKey:@"fade"];
[NSTimer scheduledTimerWithTimeInterval:0.9 target:self selector:@selector(removeFromSuperlayer) userInfo:nil repeats:NO];
}
//默认设置
-(void)DefaultSetting
{
//设置出现时间为永远
[self setEmitterTime:-1.0f];
//设置在中间发射
[self setFromPositonMode:PaticleFromePositionModeCenter];
//设置发射尺寸为全屏幕
[self setEmitterSize:CGSizeMake(320*2.0, 0)];
//设置发射模式
[self setEmitterMode:kCAEmitterLayerOutline];
//设置发射形状,默认圆形
[self setEmitterShape:kCAEmitterLayerLine];
//设置阴影透明度
[self setShadowOpacity:1.0];
//设置半径
[self setShadowRadius:0.0];
//
[self setShadowOffset:CGSizeMake(0.0, 1.0)];
//
[self setShadowColor:[[UIColor whiteColor]CGColor]];
//
[self setSeed:arc4random()%30+1];
//跟粒子
CAEmitterCell* rootCell = [CAEmitterCell emitterCell];
[rootCell setBirthRate:5.0];
[rootCell setVelocity:0];
[rootCell setLifetime:0.35];
//Cells
NSArray* children = [self getImageArray];
if (rootCell) {
[rootCell setEmitterCells:children];
[self setEmitterCells:@[rootCell]];
}
else
{
[self setEmitterCells:children];
}
}
-(CAEmitterCell *)createCellWithImage:(UIImage *)image{
CAEmitterCell *cellLayer = [CAEmitterCell emitterCell];
cellLayer.birthRate= 5.0;
cellLayer.lifetime= 5.0;
cellLayer.velocity= -100;// falling down slowly
cellLayer.velocityRange = 0;
cellLayer.yAcceleration = 2;
cellLayer.emissionRange = 0.5 * M_PI;// some variation in angle
cellLayer.spinRange= 0.25 * M_PI;// slow spin
cellLayer.contents= (id)[image CGImage];
cellLayer.color= [[UIColor colorWithRed:0.600 green:0.658 blue:0.743 alpha:1.000] CGColor];
return cellLayer;
}
-(NSArray*)getImageArray
{
NSMutableArray* array = [NSMutableArray array];
for (NSString* imageName in _imageNameArray) {
UIImage* image = [UIImage imageNamed:imageName];
if (image) {
CAEmitterCell* cell = [self createCellWithImage:image];
[array addObject:cell];
}
}
return array;
}
-(void)begin
{
if (_emitterTime > 0) {
[NSTimer scheduledTimerWithTimeInterval:_emitterTime target:self selector:@selector(fade) userInfo:nil repeats:NO];
}
}
-(IBAction)Render
:(id)sender
{
PaticleLayer* layer = [[PaticleLayer alloc]initWithImageName:@"birthday.png"];
[layer setEmitterShape:kCAEmitterLayerLine];
[layer setEmitterTime:10.0];
[self.view.layer addSublayer:layer];
[layer begin];
}