iOS CAEmitterLayer运用

CAEmitterLayer 和 CAEmitterCell是完成粒子动画的两个类,上代码演示一下大概怎么用的:

- (void)addEmitter {
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height)];
    imageView.image = [UIImage imageNamed:@"huajiTree"];
    imageView.contentMode = UIViewContentModeScaleToFill;
    [self.view addSubview:imageView];
    
 
    CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
    //例子发射位置
    emitterLayer.emitterPosition = CGPointMake(self.view.center.x, 120);
    //发射源的尺寸大小
    emitterLayer.emitterSize = CGSizeMake(100,1);
    //发射模式
    emitterLayer.emitterMode = kCAEmitterLayerSurface;
    //发射源的形状
    emitterLayer.emitterShape = kCAEmitterLayerLine;
    
    //创建粒子
    CAEmitterCell *huajiCell = [CAEmitterCell emitterCell];
    //粒子的名字
    huajiCell.name = @"huaji";
    //粒子参数的速度乘数因子
    huajiCell.birthRate = 3.0;
    huajiCell.lifetime = 20.0;
    //粒子速度
    huajiCell.velocity =10.0;
    //粒子的速度范围
    huajiCell.velocityRange = 10;
    //粒子y方向的加速度分量
    huajiCell.yAcceleration = 5;
    //周围发射角度
    huajiCell.emissionRange = 0.8 * M_PI;
    //子旋转角度范围
    huajiCell.spinRange = 0.1 * M_PI;
    huajiCell.contents = (id)[[UIImage imageNamed:@"image_emoticon25"] CGImage];

    
    emitterLayer.emitterCells = [NSArray arrayWithObjects:huajiCell,nil];
    [imageView.layer insertSublayer:emitterLayer atIndex:0];
}

如果要从一个点发射,就改成这样:

    //发射源的尺寸大小
    emitterLayer.emitterSize = CGSizeMake(1,1);
    //发射源的形状
    emitterLayer.emitterShape = kCAEmitterLayerPoint;

Demo地址,附上效果图:手动滑稽

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 学习及实践笔记 记录iOS动画的学习及实践 目录 显示层(UIView)动画初级动画关键帧动画逐帧动画Gif动画的...
    Tr2e阅读 7,492评论 8 45
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,986评论 25 709
  • 今天机缘巧合想起来看书,过敏性紫癜,ITP,这是一种免疫病,可有遗传,主要是因为血液里面的血小板数量减少,所以受到...
    l晴洛洛阅读 1,792评论 2 0
  • 四天的时间,从开封出发到青岛又回到了开封;不长的游记,从家乡记叙至海滨又重返了故乡。六七座城,走走停停;八九条路,...
    周卜言阅读 1,607评论 0 0
  • 这个时代,挤挤攘攘,每个成年人都疲于奋力拼杀。连孩童都是如此,人太多了,每个人好像都要彼此碾压,才能获取更多的生存...
    柚子红叉阅读 2,435评论 0 1

友情链接更多精彩内容