iOS---使用CAEmitterLayer制作发射粒子的特效
来源:互联网 发布:淘宝店铺公告模板 编辑:程序博客网 时间:2024/05/07 07:11
CAEmitterLayer是QuartzCore提供的粒子引擎, 可用于制作美观的粒子特效.
如下图是一个粒子特效的截图, 实际的动态效果会更好, 因为gif制作比较模糊就没有贴出来.
CAEmitterLayer
CAEmitterLayer与CAEAGLLayer类似, 如果放在UIView中则要重写UIView的layerClass方法:
@interface ViewCAEmitter : UIView@end@implementation ViewCAEmitter+ (Class)layerClass { return [CAEmitterLayer class];}// xxx@end
如果是在UIViewController中, 则直接在layer上添加CAEmitterLayer即可:
@implementation UIViewController- (void)setupCAEmitterLayer { _caEmitterLayer = [CAEmitterLayer layer]; _caEmitterLayer.frame = self.view.frame; _caEmitterLayer.opaque = YES; [self.view.layer addSublayer:_caEmitterLayer];}// xxx@end
添加CAEmitterLayer到view.layer中:
CAEmitterLayer *emitter = [CAEmitterLayer layer];emitter.frame = self.bounds;[self.layer addSublayer:emitter];// 配置emitteremitter.renderMode = kCAEmitterLayerAdditive; // 粒子如何混合, 这里是直接重叠emitter.emitterPosition = position; // 发射点的位置
CAEmitterLayer其实是装载CAEmitterCell的容器, 有一个属性emitterCells, 将CAEmitterCell直接添加到该数组中, 即可实现粒子特效了.
CAEmitterCell
CAEmitterCell用来表示一个个的粒子, 它有一系列的参数用于设置效果.
// create a particle templateCAEmitterCell *cell = [[CAEmitterCell alloc] init];cell.contents = (__bridge id)[UIImage imageNamed:imageName].CGImage; // 粒子中的图片cell.yAcceleration = -10.f; // 粒子的初始加速度cell.xAcceleration = -110.f;cell.birthRate = 2.f; // 每秒生成粒子的个数cell.lifetime = 6.f; // 粒子存活时间cell.alphaSpeed = -0.3f; // 粒子消逝的速度cell.velocity = 30.f; // 粒子运动的速度均值cell.velocityRange = 30.f; // 粒子运动的速度扰动范围cell.emissionRange = M_PI * 10.f; // 粒子发射角度, 这里是一个扇形.// add particle template to emitteremitter.emitterCells = @[cell]; // 将粒子组成的数组赋值给CAEmitterLayer的emitterCells属性即可.
关于速度等属性, 可以设置一个均值和一个扰动范围:
Each layer has its own random number generator state. Emitter cell properties that are defined as a mean and a range, such as a cell’s speed, the value of the properties are uniformly distributed in the interval [M - R/2, M + R/2].
Demo
可参考Demo:
DemoCAEmitterLayer
0 0
- iOS---使用CAEmitterLayer制作发射粒子的特效
- iOS-利用粒子发射器(CAEmitterLayer) 制作发射动画
- iOS开发进阶 - CAEmitterLayer,粒子发射系统
- iOS CAEmitterLayer 和 CAEmitterCell 粒子发射
- iOS CAEmitterLayer 粒子发射器的使用详解
- CAEmitterLayer 粒子发射Layer的相关属性
- CAEmitterLayer,粒子发射系统
- 粒子发射系统中的CAEmitterLayer
- 关于粒子发射(CAEmitterLayer)
- CAEmitterLayer CAEmitterCell 一个基于Core Animation的粒子发射系统
- IOS 使用 CAEmitterLayer实现粒子效果
- 使用带粒子效果的 CAEmitterLayer
- iOS 粒子效果 CAEmitterCell CAEmitterLayer
- ios粒子效果CAEmitteCell CAEmitterLayer
- iOS CAEmitterLayer 实现粒子效果
- iOS 粒子效果实现CAEmitterLayer
- iOS 烟花撒花效果,图层渐变,图层倒影特效。CAEmitterLayer粒子发射器
- 【插件】特效发射粒子数查找工具
- WebDriver切换浏览器窗口
- No such property: GROUP for class: org.gradle.api.publication.maven.internal.deployer
- Android ABI
- 组合求和(C++)
- error C2361: initialization of 'rect' is skipped by 'default' label
- iOS---使用CAEmitterLayer制作发射粒子的特效
- 数据连接池
- Cocos2d-Lua之面向对象的封装
- android中的sqlite查询数据去掉重复值
- linux下打开、关闭tomcat,实时查看tomcat运行日志
- 学习笔记
- Python day01周
- linux CentOS 6忘记密码的解决办法
- SQL查询关键字位置并截取;查询重复;