您的位置:首页 > 其它

CAEmitterLayer 粒子效果

2015-07-09 22:43 375 查看

CAEmitterLayer 粒子效果

CAEmitterLayer运行在GPU上,不消耗cpu。

[code] //创建出Layer
    CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];

    //显示边框
    emitterLayer.borderWidth = 1.f;
    emitterLayer.borderColor = [[UIColor redColor] CGColor];

    //绑定尺寸
    emitterLayer.frame = CGRectMake(100, 100, 100, 100);
     //发射源的尺寸大小
 // snowEmitter.emitterSize = CGSizeMake(self.view.bounds.size.width * 20, 20);

    //发射点
    emitterLayer.emitterPosition = CGPointMake(0, 0);

    //发射模式
    emitterLayer.emitterMode = kCAEmitterLayerSurface;

    //发射形状
    emitterLayer.emitterShape = kCAEmitterLayerLine;

 //   emitterLayer.masksToBounds = YES; 不能超出边框
/*
snowEmitter.shadowOpacity = 1.0;
  snowEmitter.shadowRadius = 0.0;
  snowEmitter.shadowOffset = CGSizeMake(0.0, 1.0);
  //粒子边缘的颜色
  snowEmitter.shadowColor = [[UIColor redColor] CGColor];
  snowEmitter.emitterCells = [NSArray arrayWithObjects:snowflake,snowflake1,nil];
  [self.view.layer insertSublayer:snowEmitter atIndex:0];
*/

    //添加layer
    [self.view.layer addSublayer:emitterLayer];

    //创建粒子
    CAEmitterCell *cell = [CAEmitterCell emitterCell];

    //粒子产生率,太大的话成黑色块了,
    cell.birthRate = 1.f;

    //粒子生命周期,太小的话,出生就死掉了
    cell.lifetime = 120.f;

    //速度值
    cell.velocity = 10;

    //速度值的微调值7-13
    cell.velocityRange = 3.f;

    //y轴加速度,相当于有重力了
    cell.yAcceleration = 2.f;

    //设置粒子颜色
    cell.color = [UIColor blackColor].CGColor;

    //发射角度
    cell.emissionRange = 3.1f * M_1_PI;
     //子旋转角度范围
 // snowflake.spinRange = 0.25 * M_PI;
    //设置图片
    cell.contents = (__bridge id)([UIImage imageNamed:@"snow"].CGImage);

    //让CAEmitterLayer与CAEmitterCell产生关联
    emitterLayer.emitterCells = @[cell];


下面是 CAEmitterLayer和CAEmitterCell 的一些属性:

CAEmitterLayer

提供了一个基于 Core Animation 的粒子 发 射系 统 ,粒子用 CAEmitterCell 来初始化。 粒子画在背景 层 盒 边 界上

Properties:

birthRate: 粒子 产 生系数,默 认 1.0 ;

emitterCells: 装着 CAEmitterCell 对 象的数 组 ,被用于把粒子投放到 layer 上;

emitterDepth: 决定粒子形状的深度 联 系: emitter shape

emitterMode: 发 射模式

- NSString * const kCAEmitterLayerPoints;

- NSString * const kCAEmitterLayerOutline;

- NSString * const kCAEmitterLayerSurface;

- NSString * const kCAEmitterLayerVolume;

emitterPosition: 发 射位置

emitterShape: 发 射源的形状:

- NSString * const kCAEmitterLayerPoint;

- NSString * const kCAEmitterLayerLine;

- NSString * const kCAEmitterLayerRectangle;

- NSString * const kCAEmitterLayerCuboid;

- NSString * const kCAEmitterLayerCircle;

- NSString * const kCAEmitterLayerSphere;

emitterSize: 发 射源的尺寸大;

emitterZposition: 发 射源的 z 坐 标 位置;

lifetime: 粒子生命周期

preservesDepth: 不是多很清楚(粒子是平展在 层 上)

renderMode: 渲染模式:

- NSString * const kCAEmitterLayerUnordered;

- NSString * const kCAEmitterLayerOldestFirst;

- NSString * const kCAEmitterLayerOldestLast;

- NSString * const kCAEmitterLayerBackToFront;

- NSString * const kCAEmitterLayerAdditive;

scale: 粒子的缩放比例:

seed :用于初始化随机数产生的种子

spin: 自旋转速度

velocity :粒子速度

CAEmitterCell

CAEmitterCell 类 代从从 CAEmitterLayer 射出的粒子; emitter cell 定 义 了粒子 发 射的方向。

alphaRange: 一个粒子的 颜 色 alpha 能改 变 的范 围 ;

alphaSpeed: 粒子透明度在生命周期内的改变速度;

birthrate :粒子参数的速度乘数因子;

blueRange :一个粒子的 颜 色 blue 能改 变 的范 围 ;

blueSpeed: 粒子 blue 在生命周期内的改变速度;

color: 粒子的颜色

contents :是个 CGImageRef 的对象 , 既粒子要展现的图片;

contentsRect :应该画在 contents 里的子 rectangle :

emissionLatitude :发射的 z 轴方向的角度

emissionLongitude:x-y 平面的 发 射方向

emissionRange ;周 围发射角度

emitterCells :粒子发射的粒子

enabled :粒子是否被渲染

greenrange: 一个粒子的 颜 色 green 能改 变 的范 围 ;

greenSpeed: 粒子 green 在生命周期内的改变速度;

lifetime :生命周期

lifetimeRange :生命周期范围

magnificationFilter :不是很清楚好像增加自己的大小

minificatonFilter :减小自己的大小

minificationFilterBias :减小大小的因子

name :粒子的名字

redRange : 一个粒子的 颜 色 red 能改 变 的范 围 ;

redSpeed; 粒子 red 在生命周期内的改变速度;

scale :缩放比例:

scaleRange :缩放比例范围;

scaleSpeed :缩放比例速度:

spin :子旋转角度

spinrange :子旋转角度范围

style :不是很清楚:

velocity :速度

velocityRange :速度范围

xAcceleration: 粒子 x 方向的加速度分量

yAcceleration: 粒子 y 方向的加速度分量

zAcceleration: 粒子 z 方向的加速度分量

Class Methods

defauleValueForKey: 更具健 获 得 值 ;

emitterCell :初始化方法

shouldArchiveValueForKey: 是否 归 档莫 键值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: