標籤:
1、概述
什麼是UIDynamic?
UIDynamic是從iOS 7開始引入的一種新技術,隸屬於UIKit架構。
可以認為是一種物理引擎,能類比和模擬現實生活中的物理現象。比如:重力、彈性碰撞等現象。
物理引擎的價值:
(1)廣泛用於遊戲開發,經典成功案例是“憤怒的小鳥”
(2)讓開發人員可以在遠離物理學公式的情況下,實現炫酷的物理模擬效果
(3)提高了遊戲開發效率,產生更多優秀好玩的物理模擬遊戲
知名的2D物理引擎:
Box2d
Chipmunk
2、使用步驟
要想使用UIDynamic來實現物理模擬效果,大致的步驟如下:
第一步:建立一個物理模擬器(順便設定模擬範圍)。
第二步:建立相應的物理模擬行為(順便添加物理模擬元素)。
第三步:將物理模擬行為添加到物理模擬器中 à 開始模擬。
3、三大概念
物理模擬元素(Dynamic Item)
誰要進行物理模擬?
注意:
(1)不是任何對象都能做物理模擬元素
(2)不是任何對象都能進行物理模擬
這些對象才能做物理模擬元素:
(1)任何遵守了UIDynamicItem協議的對象
(2)UIView預設已經遵守了UIDynamicItem協議,因此任何UI控制項都能做物理模擬
(3)UICollectionViewLayoutAttributes類預設也遵守UIDynamicItem協議
物理模擬行為(Dynamic Behavior)
執行怎樣的物理模擬效果?怎樣的動畫效果?
UIDynamic提供了以下幾種物理模擬行為:
UIGravityBehavior:重力行為
UICollisionBehavior:碰撞行為
UISnapBehavior:捕捉行為
UIPushBehavior:推動行為
UIAttachmentBehavior:附著行為
UIDynamicItemBehavior:動力元素行為
物理模擬行為須知:
(1)上述所有物理模擬行為都繼承自UIDynamicBehavior
(2)所有的UIDynamicBehavior都可以獨立進行
(3)組合使用多種行為時,可以實現一些比較複雜的效果
物理模擬器(Dynamic Animator)
讓物理模擬元素執行具體的物理模擬行為
物理模擬器須知:
(1)它可以讓物理模擬元素執行物理模擬行為
(2)它是UIDynamicAnimator類型的對象
UIDynamicAnimator的初始化:
- (instancetype)initWithReferenceView:(UIView *)view;
view參數:是一個參照視圖,表示物理模擬的範圍
UIDynamicAnimator的常見方法:
- (void)addBehavior:(UIDynamicBehavior *)behavior;
添加1個物理模擬行為
- (void)removeBehavior:(UIDynamicBehavior *)behavior;
移除1個物理模擬行為
- (void)removeAllBehaviors;
移除之前添加過的所有物理模擬行為
UIDynamicAnimator的常見屬性:
@property (nonatomic, readonly) UIView* referenceView;
參照視圖
@property (nonatomic, readonly, copy) NSArray* behaviors;
添加到物理模擬器中的所有物理模擬行為
@property (nonatomic, readonly, getter = isRunning) BOOL running;
是否進行中物理模擬
@property (nonatomic, assign) id <UIDynamicAnimatorDelegate> delegate;
代理對象(能監聽物理模擬器的模擬過程,比如開始和結束)
4、重力行為(UIGravityAnimator)
簡介:
給定重力方向、加速度,讓物體朝著重力方向掉落
UIGravityBehavior的初始化:
- (instancetype)initWithItems:(NSArray *)items;
item參數 :裡面存放著物理模擬元素
UIGravityBehavior常見方法:
- (void)addItem:(id <UIDynamicItem>)item;
添加1個物理模擬元素
- (void)removeItem:(id <UIDynamicItem>)item;
移除1個物理模擬元素
UIGravityBehavior常見屬性
@property (nonatomic, readonly, copy) NSArray* items;
添加到重力行為中的所有物理模擬元素
@property (readwrite, nonatomic) CGVector gravityDirection;
重力方向(是一個二維向量)
@property (readwrite, nonatomic) CGFloat angle;
重力方向(是一個角度,以x軸正方向為0°,順時針正數,逆時針負數)
@property (readwrite, nonatomic) CGFloat magnitude;
量級(用來控制加速度,1.0代表加速度是1000 points /second²)
例如,使一個UIImageView往下落效果
設定成員屬性:
@property (nonatomic, strong) UIDynamicAnimator *animator;
//第一步:建立一個物理模擬器(順便設定模擬範圍)。
self.animator =
[[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//第二步:建立相應的物理模擬行為(順便添加物理模擬元素)。
UIGravityBehavior *gravity =
[[UIGravityBehavior alloc] initWithItems:@[self.blueView]];
//第三步:將物理模擬行為添加到物理模擬器中 à 開始模擬。
[self.animator addBehavior:gravity];
還可以將碰撞行為與上面重力行為合成一個動畫,實現UIImageView落到螢幕最下邊時加一個彈跳效果:
//建立物理模擬行為--->重力行為(items: 物理模擬元素)
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.blueView]];
//碰撞檢測行為
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.blueView]];//可以添加多個,比如@[self.blueView,self.blueView2, self.blueView3]
// 讓參照視圖的bounds成為碰撞檢測的邊框
collision.translatesReferenceBoundsIntoBoundary = YES;
//添加 物理模擬行為 到 物理模擬器
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
重點知識點:
如果讓你直接寫,你或許會這樣寫:
UIDynamicAnimator *animator =
[[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior *gravity =
[[UIGravityBehavior alloc] initWithItems:@[self.blueView]];
[animator addBehavior:gravity];
上面代碼不會實現預料效果,因為執行完上面代碼後animator會立即被銷毀,來不及實現動畫效果物理模擬器對象就被銷毀了。這就是上面為什麼把模擬器設定為成員屬性的原因。這是個重要知識點,在其他方面也會經常遇到。
5、碰撞行為(UICollisionBehavior)
簡介:
(1)可以讓物體之間實現碰撞效果
(2)可以通過添加邊界(boundary),讓物理碰撞局限在某個空間中
UICollisionBehavior邊界相關的方法:
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier forPath:(UIBezierPath*)bezierPath;
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2;
- (UIBezierPath*)boundaryWithIdentifier:(id <NSCopying>)identifier;
- (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier;
@property (nonatomic, readonly, copy) NSArray* boundaryIdentifiers;
- (void)removeAllBoundaries;
UICollisionBehavior常見用法:
@property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;
是否以參照視圖的bounds為邊界
- (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:
(UIEdgeInsets)insets;
設定參照視圖的bounds為邊界,並且設定內邊距
@property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;
碰撞模式(分為3種,元素碰撞、邊界碰撞、全體碰撞)
@property (nonatomic, assign, readwrite) id <UICollisionBehaviorDelegate> collisionDelegate;
代理對象(可以監聽元素的碰撞過程)
6、捕捉行為(UISnapBehavior)
簡介:
可以讓物體迅速衝到某個位置(捕捉位置),捕捉到位置之後會帶有一定的震動
UISnapBehavior的初始化:
- (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:
(CGPoint)point;
UISnapBehavior常見屬性:
@property (nonatomic, assign) CGFloat damping;
用於減幅、減震(取值範圍是0.0 ~ 1.0,值越大,震動幅度越小)
UISnapBehavior使用注意:
如果要進行連續的捕捉行為,需要先把前面的捕捉行為從物理模擬器中移除
iOS開發之UIDynamic