CAEmitterLayer 粒子效果

CAEmitterLayer 粒子效果


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

 //创建出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: 是否 归 档莫 键值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 `CAEmitterLayer` 可以很方便地实现各种粒子效果,下面是使用 `CAEmitterLayer` 实现不同天气效果的示例代码: ### 小雨效果 ```swift let emitterLayer = CAEmitterLayer() emitterLayer.emitterShape = .line emitterLayer.emitterSize = CGSize(width: view.bounds.width, height: 1) let rain = makeRainCell() emitterLayer.emitterCells = [rain] view.layer.addSublayer(emitterLayer) func makeRainCell() -> CAEmitterCell { let cell = CAEmitterCell() cell.contents = UIImage(named: "raindrop")?.cgImage cell.birthRate = 50 cell.lifetime = 2 cell.velocity = 100 cell.velocityRange = 50 cell.yAcceleration = 500 cell.scale = 0.2 cell.scaleRange = 0.1 cell.emissionLongitude = -.pi cell.emissionRange = .pi / 4 return cell } ``` ### 中雨效果 ```swift let emitterLayer = CAEmitterLayer() emitterLayer.emitterShape = .line emitterLayer.emitterSize = CGSize(width: view.bounds.width, height: 1) let rain = makeRainCell() emitterLayer.emitterCells = [rain] view.layer.addSublayer(emitterLayer) func makeRainCell() -> CAEmitterCell { let cell = CAEmitterCell() cell.contents = UIImage(named: "raindrop")?.cgImage cell.birthRate = 100 cell.lifetime = 2 cell.velocity = 150 cell.velocityRange = 50 cell.yAcceleration = 500 cell.scale = 0.3 cell.scaleRange = 0.1 cell.emissionLongitude = -.pi cell.emissionRange = .pi / 4 return cell } ``` ### 暴雨效果 ```swift let emitterLayer = CAEmitterLayer() emitterLayer.emitterShape = .line emitterLayer.emitterSize = CGSize(width: view.bounds.width, height: 1) let rain = makeRainCell() emitterLayer.emitterCells = [rain] view.layer.addSublayer(emitterLayer) func makeRainCell() -> CAEmitterCell { let cell = CAEmitterCell() cell.contents = UIImage(named: "raindrop")?.cgImage cell.birthRate = 200 cell.lifetime = 2 cell.velocity = 200 cell.velocityRange = 50 cell.yAcceleration = 500 cell.scale = 0.4 cell.scaleRange = 0.1 cell.emissionLongitude = -.pi cell.emissionRange = .pi / 4 return cell } ``` ### 下雪效果 ```swift let emitterLayer = CAEmitterLayer() emitterLayer.emitterShape = .line emitterLayer.emitterSize = CGSize(width: view.bounds.width, height: 1) let snow = makeSnowCell() emitterLayer.emitterCells = [snow] view.layer.addSublayer(emitterLayer) func makeSnowCell() -> CAEmitterCell { let cell = CAEmitterCell() cell.contents = UIImage(named: "snowflake")?.cgImage cell.birthRate = 50 cell.lifetime = 10 cell.velocity = 50 cell.velocityRange = 20 cell.yAcceleration = 10 cell.scale = 0.2 cell.scaleRange = 0.1 cell.emissionLongitude = -.pi cell.emissionRange = .pi / 4 return cell } ``` ### 沙尘暴效果 ```swift let emitterLayer = CAEmitterLayer() emitterLayer.emitterShape = .line emitterLayer.emitterSize = CGSize(width: view.bounds.width, height: 1) let dust = makeDustCell() emitterLayer.emitterCells = [dust] view.layer.addSublayer(emitterLayer) func makeDustCell() -> CAEmitterCell { let cell = CAEmitterCell() cell.contents = UIImage(named: "dust")?.cgImage cell.birthRate = 100 cell.lifetime = 5 cell.velocity = 100 cell.velocityRange = 50 cell.yAcceleration = 10 cell.scale = 0.2 cell.scaleRange = 0.1 cell.emissionLongitude = -.pi cell.emissionRange = .pi / 4 return cell } ``` ### 高温效果 ```swift let emitterLayer = CAEmitterLayer() emitterLayer.emitterShape = .line emitterLayer.emitterSize = CGSize(width: view.bounds.width, height: 1) let fire = makeFireCell() emitterLayer.emitterCells = [fire] view.layer.addSublayer(emitterLayer) func makeFireCell() -> CAEmitterCell { let cell = CAEmitterCell() cell.contents = UIImage(named: "fire")?.cgImage cell.birthRate = 100 cell.lifetime = 1 cell.velocity = 50 cell.velocityRange = 20 cell.yAcceleration = -50 cell.scale = 0.2 cell.scaleRange = 0.1 cell.color = UIColor.orange.cgColor cell.emissionLongitude = .pi cell.emissionRange = .pi / 4 return cell } ``` 以上代码中,每个天气效果都是通过创建一个 `CAEmitterLayer` 和一个或多个 `CAEmitterCell` 来实现的。在每个 `CAEmitterCell` 中,可以设置粒子的属性,例如:出生率、寿命、速度、加速度、缩放比例、颜色等等。可以根据具体需求自由调整每个属性的值,以达到想要的效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值