Core Animation - CAEmitterLayer 粒子效果
CAEmitterLayer 是一個(gè)高性能的粒子引擎蘸朋,被用來(lái)創(chuàng)建復(fù)雜的粒子動(dòng)畫(huà)如:煙霧次酌,火葫录,雨等效果挺邀,并且有很高的性能蝠嘉。
CAEmitterLayer 看上去像是許多 CAEmitterCell 的容器缴允,這些 CAEmitterCell 定義了一個(gè)例子效果荚守。你將會(huì)為不同的例子效果定義一個(gè)或多個(gè) CAEmitterCell 作為模版,同時(shí) CAEmitterLayer 負(fù)責(zé)基于這些模版實(shí)例化一個(gè)粒子流练般。一個(gè) CAEmitterCell 類似于一個(gè) CALayer :它有一個(gè) contents 屬性可以定義為一個(gè) CGImage 矗漾,另外還有一些可設(shè)置屬性控制著表現(xiàn)和行為。
使用步驟 :創(chuàng)建 CAEmitterLayer 薄料,然后創(chuàng)建 CAEmitterCell 敞贡,將cells 添加到 CAEmitterLayer 的 emitterCells屬性中 ,最后 CAEmitterLayer 圖層插入到視圖層次的最前面摄职。
CAEmitterLayer 屬性說(shuō)明:
- emitterPosition 發(fā)射源位置
- emitterSize 發(fā)射源大小
- emitterShape 發(fā)射源的形狀
用的比較多的是 kCAEmitterLayerLine 和 kCAEmitterLayerPoint誊役。這決定了你的粒子從一個(gè)點(diǎn)噴出來(lái)的,還是從一條線上每個(gè)點(diǎn)噴出來(lái)的谷市,前者像焰火蛔垢,后者像瀑布。
- emitterMode 發(fā)射源的模式
CAEmitterCell 屬性說(shuō)明:
- birthRate 每秒生成多少個(gè)粒子
- lifetime 存活時(shí)間
- velocityRange 粒子平均初始速度正數(shù)表示向上迫悠,負(fù)數(shù)表示向下
- yAcceleration 決定了每個(gè)方向上的粒子加速度
- spinRange 粒子平均的旋轉(zhuǎn)速度
- scale 圖片比例
其他:UIDynamic
1. UIDynamic 物理仿真行為,基本的動(dòng)力學(xué)行為包括 UIGravityBehavior鹏漆、UICollisionBehavior、UIAttachmentBehavior、UISnapBehavior甫男、UIPushBehavior 以及 UIDynamicItemBehavior,可以組合使用且改。
2. 不是任何對(duì)象都可以做物理仿真效果 物理仿真元素要素:任何遵守了 UIDynamicItem 協(xié)議的對(duì)象 UIView默認(rèn)已經(jīng)遵守了UIDynamicItem 協(xié)議,因此任何UI控件都能做物理仿真验烧。
3. 使用步驟
- 創(chuàng)建一個(gè)UIDynamicAnimator對(duì)象
- 創(chuàng)建行為對(duì)象(UIDynamicBehavior)
- 將要執(zhí)行動(dòng)畫(huà)的對(duì)象添加到UIDynamicBehavior中
重點(diǎn)介紹下面 Demo 中用到的 UIGravityBehavior 和UICollisionBehavior :
重力行為(Gravity)
//1 創(chuàng)建物理仿真器
UIDynamicAnimator *animator = [[UIDynamicAnimator alloc]
initWithReferenceView:self.view];
self.animator = animator;
//2 創(chuàng)建重力行為(物理行為)
UIGravityBehavior *behavior = [[UIGravityBehavior alloc]
initWithItems:@[self.redView]];
//量級(jí)(用來(lái)控制加速度,1.0代表加速度是1000 points/second2)
behavior.magnitude = 0.2;
//方向
behavior.gravityDirection = CGVectorMake(1, 1);
behavior.angle = 0;
//3 把物理行為添加到物理仿真器中 開(kāi)始動(dòng)畫(huà)
[animator addBehavior:behavior];
碰撞行為(Collision)
碰撞邊界的設(shè)定
- setTranslatesReferenceBoundsIntoBoundaryWithInsets 給定一個(gè)UIEdgeInsets板驳。
- addBoundaryWithIdentifier:fromPoint: toPoint: 傳入兩點(diǎn)坐標(biāo)添加邊界。
- addBoundaryWithIdentifier : forPath: 使用 UIBezierPath 方式添加邊界碍拆。
碰撞邊界的代理方法
設(shè)置 collisionDelegate 可以監(jiān)測(cè)到兩個(gè)物體或者物體與邊界碰撞的過(guò)程若治。
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p;
- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2;
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p;
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier;