CAEmitterLayer

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

    // 显示边框
    emitterLayer.borderWidth = 1.f;

    // 给定尺寸
    emitterLayer.frame = CGRectMake(100, 100, 100, 100);

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

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

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

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




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

    // 粒子产生率
    cell.birthRate = 10.f;

    // 粒子生命周期
    cell.lifetime = 10.f;

    // 速度值
    cell.velocity = 10;

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

    // y轴加速度
    cell.yAcceleration = 2.f;

    // 发射角度
    cell.emissionRange = 4.f * M_1_PI;

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

    // 设置图片
    cell.contents = (__bridge id)([UIImage imageNamed:@"snow"].CGImage);




    // 让CAEmitterCell与CAEmitterLayer产生关联
    emitterLayer.emitterCells = @[cell];
  • 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、付费专栏及课程。

余额充值