UIDynamic物理仿真 下面介紹一下各種屬性
一、簡(jiǎn)單介紹
1.什么是UIDynamic
UIDynamic是從iOS 7開(kāi)始引入的一種新技術(shù),隸屬于UIKit框架,可以認(rèn)為是一種物理引擎,能模擬和仿真現(xiàn)實(shí)生活中的物理現(xiàn)象
如:重力、彈性碰撞等現(xiàn)象
2.物理引擎的價(jià)值
廣泛用于游戲開(kāi)發(fā),經(jīng)典成功案例是“憤怒的小鳥(niǎo)”
讓開(kāi)發(fā)人員可以在遠(yuǎn)離物理學(xué)公式的情況下,實(shí)現(xiàn)炫酷的物理仿真效果
提高了游戲開(kāi)發(fā)效率,產(chǎn)生更多優(yōu)秀好玩的物理仿真游戲
3.知名的2D物理引擎
Box2d
Chipmunk
二、使用步驟
要想使用UIDynamic來(lái)實(shí)現(xiàn)物理仿真效果,大致的步驟如下
(1)創(chuàng)建一個(gè)物理仿真器(順便設(shè)置仿真范圍)
(2)創(chuàng)建相應(yīng)的物理仿真行為(順便添加物理仿真元素)
(3)將物理仿真行為添加到物理仿真器中 ? 開(kāi)始仿真
三、相關(guān)說(shuō)明
1.三個(gè)概念
(1)誰(shuí)要進(jìn)行物理仿真?
物理仿真元素(Dynamic Item)
(2)執(zhí)行怎樣的物理仿真效果?怎樣的動(dòng)畫(huà)效果?
物理仿真行為(Dynamic Behavior)
(3)讓物理仿真元素執(zhí)行具體的物理仿真行為
物理仿真器(Dynamic Animator)
2.物理仿真元素
注意:
不是任何對(duì)象都能做物理仿真元素
不是任何對(duì)象都能進(jìn)行物理仿真
物理仿真元素要素:
任何遵守了UIDynamicItem協(xié)議的對(duì)象
UIView默認(rèn)已經(jīng)遵守了UIDynamicItem協(xié)議,因此任何UI控件都能做物理仿真
UICollectionViewLayoutAttributes類默認(rèn)也遵守UIDynamicItem協(xié)議
四、物理仿真器 ------------UIDynamicAnimator
須知:它可以讓物理仿真元素執(zhí)行物理仿真行為,它是UIDynamicAnimator類型的對(duì)象
物理仿真器屬性方法說(shuō)明
(1)UIDynamicAnimator的常見(jiàn)方法
- (instancetype)initWithReferenceView:(UIView *)view; //初始化一個(gè)仿真器。view參數(shù):是一個(gè)參照視圖,表示物理仿真的范圍
- (void)addBehavior:(UIDynamicBehavior *)behavior; //添加1個(gè)物理仿真行為
- (void)removeBehavior:(UIDynamicBehavior *)behavior; //移除1個(gè)物理仿真行為
- (void)removeAllBehaviors; //移除之前添加過(guò)的所有物理仿真行為
- (NSArray<id<UIDynamicItem>> *)itemsInRect:(CGRect)rect;
- (void)updateItemUsingCurrentState:(id <UIDynamicItem>)item;//(更新dynamic item在UIDynamicAnimator內(nèi)部的代表的狀態(tài)),dynamic item被加入到UIDynamicAnimator后,你更改了dynamic item的狀態(tài),你應(yīng)該使用這個(gè)方法更新dynamic item的狀態(tài)。
- (NSTimeInterval)elapsedTime; //返回動(dòng)畫(huà)開(kāi)始到現(xiàn)在的時(shí)間間隔
(2)UIDynamicAnimator的常見(jiàn)屬性
@property (nonatomic, readonly) UIView* referenceView; //參照視圖
@property (nonatomic, readonly, copy) NSArray* behaviors;//添加到物理仿真器中的所有物理仿真行為
@property (nonatomic, readonly, getter = isRunning) BOOL running;//是否正在進(jìn)行物理仿真
@property (nonatomic, assign) id <UIDynamicAnimatorDelegate> delegate;//代理對(duì)象(能監(jiān)聽(tīng)物理仿真器的仿真過(guò)程,比如開(kāi)始和結(jié)束){
@optional
- (void)dynamicAnimatorWillResume:(UIDynamicAnimator *)animator;
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator;
}
(3)UIDynamicAnimator中的Collection View Additions:
? - (UICollectionViewLayoutAttributes *)layoutAttributesForCellAtIndexPath:(NSIndexPath *)indexPath;//返回指定位置collection view cell的布局屬性
? - (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind atIndexPath:(NSIndexPath *)indexPath; //返回indexPath位置上collection view cell的指定(用decorationViewKind指定)decorationView的布局屬性。
? - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;//返回indexPath位置上collection view cell的指定(用kind指定)SupplementaryView的布局屬性。
五、物理仿真行為 ------------UIDynamicBehavior
(1)UIDynamic提供了以下幾種物理仿真行為
UIGravityBehavior:重力行為
UICollisionBehavior:碰撞行為
UISnapBehavior:捕捉行為
UIPushBehavior:推動(dòng)行為
UIAttachmentBehavior:附著行為
UIDynamicItemBehavior:動(dòng)力元素行為
(2)物理仿真行為須知
上述所有物理仿真行為都繼承自UIDynamicBehavior,所有的UIDynamicBehavior都可以獨(dú)立進(jìn)行,組合使用多種行為時(shí),可以實(shí)現(xiàn)一些比較復(fù)雜的效果。
(3)UIDynamicBehavior屬性方法介紹
- (void)addChildBehavior:(UIDynamicBehavior *)behavior;
- (void)removeChildBehavior:(UIDynamicBehavior *)behavior;
@property (nonatomic, readonly, copy) NSArray<__kindof UIDynamicBehavior *> *childBehaviors;
@property (nullable, nonatomic,copy) void (^action)(void);
- (void)willMoveToAnimator:(nullable UIDynamicAnimator *)dynamicAnimator;
@property (nullable, nonatomic, readonly) UIDynamicAnimator *dynamicAnimator;
UIGravityBehavior(重力)
- (instancetype)initWithItems:(NSArray<id <UIDynamicItem>> *)items;//初始化
- (void)addItem:(id <UIDynamicItem>)item;//添加
- (void)removeItem:(id <UIDynamicItem>)item;//移除
@property (nonatomic, readonly, copy) NSArray<id <UIDynamicItem>> *items;//行為數(shù)組
@property (readwrite, nonatomic) CGVector gravityDirection;//運(yùn)動(dòng)方向,默認(rèn)是(0.0f, 1.0f)。dx為-1.0f時(shí)向左運(yùn)動(dòng),dy為-1.0時(shí)向上運(yùn)動(dòng),所以根據(jù)0.0~1.0可以定位所有的方向。
@property (readwrite, nonatomic) CGFloat angle;//重力方向(是一個(gè)角度,以x軸正方向?yàn)?°,順時(shí)針正數(shù),逆時(shí)針負(fù)數(shù))
@property (readwrite, nonatomic) CGFloat magnitude;/量級(jí)(用來(lái)控制加速度,1.0代表加速度是1000 points /second2)可以去負(fù)數(shù)
- (void)setAngle:(CGFloat)angle magnitude:(CGFloat)magnitude;//
UICollisionBehavior(碰撞)
- (instancetype)initWithItems:(NSArray<id <UIDynamicItem>> *)items;//初始化
- (void)addItem:(id <UIDynamicItem>)item;//添加
- (void)removeItem:(id <UIDynamicItem>)item;//移除
@property (nonatomic, readonly, copy) NSArray<id <UIDynamicItem>> *items;//碰撞行為數(shù)組
@property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;//碰撞模式(分三種,
UICollisionBehaviorModeItems = 1 << 0,元素碰撞
UICollisionBehaviorModeBoundaries = 1 << 1, 邊界碰撞
UICollisionBehaviorModeEverything = NSUIntegerMax 全體碰撞)
@property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;//是否以參照視圖的bounds為邊界
@property (nullable, nonatomic, weak, readwrite) id <UICollisionBehaviorDelegate> collisionDelegate;//代理
{-------------------------------------代理方法
- (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;
// The identifier of a boundary created with translatesReferenceBoundsIntoBoundary or setTranslatesReferenceBoundsIntoBoundaryWithInsets is nil
- (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;
}
- (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets;//
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier forPath:(UIBezierPath *)bezierPath;//添加一個(gè)path作為碰撞邊界
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2;//添加一條線作為碰撞邊界
- (nullable UIBezierPath *)boundaryWithIdentifier:(id <NSCopying>)identifier;//
- (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier; //移除某個(gè)的
@property (nullable, nonatomic, readonly, copy) NSArray<id <NSCopying>> *boundaryIdentifiers;//
- (void)removeAllBoundaries;//移除所有
UISnapBehavior(捕捉)
- (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:(CGPoint)point;//初始化
@property (nonatomic, assign) CGPoint snapPoint;
@property (nonatomic, assign) CGFloat damping;//用于減幅、減震(取值范圍是0.0 ~ 1.0,值越大,震動(dòng)幅度越?。?注意:
//2.執(zhí)行捕捉行為
43 //注意:這個(gè)控件只能用在一個(gè)仿真行為上,如果要擁有持續(xù)的仿真行為,那么需要把之前的所有仿真行為刪除
44 //刪除之前的所有仿真行為
45 [self.animator removeAllBehaviors];
46 [self.animator addBehavior:snap];
UIPushBehavior(推動(dòng))
- (instancetype)initWithItems:(NSArray<id <UIDynamicItem>> *)items mode:(UIPushBehaviorMode)mode;
- (void)addItem:(id <UIDynamicItem>)item;
- (void)removeItem:(id <UIDynamicItem>)item;
@property (nonatomic, readonly, copy) NSArray<id <UIDynamicItem>> *items;
- (UIOffset)targetOffsetFromCenterForItem:(id <UIDynamicItem>)item;//
- (void)setTargetOffsetFromCenter:(UIOffset)o forItem:(id <UIDynamicItem>)item;//
@property (nonatomic, readonly) UIPushBehaviorMode mode;//{
UIPushBehaviorModeContinuous,//持續(xù)的力
UIPushBehaviorModeInstantaneous //一次性的力
}
@property (nonatomic, readwrite) BOOL active;//默認(rèn)值是YES,將其設(shè)置為NO,可以將行為停止。
@property (readwrite, nonatomic) CGFloat angle;//角度
@property (readwrite, nonatomic) CGFloat magnitude;//加速度
@property (readwrite, nonatomic) CGVector pushDirection;//運(yùn)動(dòng)方向
- (void)setAngle:(CGFloat)angle magnitude:(CGFloat)magnitude;//
UIAttachmentBehavior(附著)
- (instancetype)initWithItem:(id <UIDynamicItem>)item attachedToAnchor:(CGPoint)point;
- (instancetype)initWithItem:(id <UIDynamicItem>)item offsetFromCenter:(UIOffset)offset attachedToAnchor:(CGPoint)point;
- (instancetype)initWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2;
- (instancetype)initWithItem:(id <UIDynamicItem>)item1 offsetFromCenter:(UIOffset)offset1 attachedToItem:(id <UIDynamicItem>)item2 offsetFromCenter:(UIOffset)offset2;
+ (instancetype)slidingAttachmentWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2 attachmentAnchor:(CGPoint)point axisOfTranslation:(CGVector)axis;
+ (instancetype)slidingAttachmentWithItem:(id <UIDynamicItem>)item attachmentAnchor:(CGPoint)point axisOfTranslation:(CGVector)axis;
+ (instancetype)limitAttachmentWithItem:(id <UIDynamicItem>)item1 offsetFromCenter:(UIOffset)offset1 attachedToItem:(id <UIDynamicItem>)item2 offsetFromCenter:(UIOffset)offset2;
+ (instancetype)fixedAttachmentWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2 attachmentAnchor:(CGPoint)point;
+ (instancetype)pinAttachmentWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2 attachmentAnchor:(CGPoint)point;
@property (nonatomic, readonly, copy) NSArray<id <UIDynamicItem>> *items;
@property (readonly, nonatomic) UIAttachmentBehaviorType attachedBehaviorType;//{
UIAttachmentBehaviorTypeItems,
UIAttachmentBehaviorTypeAnchor
}
@property (readwrite, nonatomic) CGPoint anchorPoint;//
@property (readwrite, nonatomic) CGFloat length; //
@property (readwrite, nonatomic) CGFloat damping; //振幅
@property (readwrite, nonatomic) CGFloat frequency; //頻率
@property (readwrite, nonatomic) CGFloat frictionTorque;//
@property (readwrite, nonatomic) UIFloatRange attachmentRange; //
UIDynamicItemBehavior:動(dòng)力元素行為
- (instancetype)initWithItems:(NSArray<id <UIDynamicItem>> *)items;//
- (void)addItem:(id <UIDynamicItem>)item;//
- (void)removeItem:(id <UIDynamicItem>)item;//
@property (nonatomic, readonly, copy) NSArray<id <UIDynamicItem>> *items;//
@property (readwrite, nonatomic) CGFloat elasticity; //(彈性系數(shù))決定了碰撞的彈性程度,比如碰撞時(shí)物體的彈性
@property (readwrite, nonatomic) CGFloat friction;//(摩擦系數(shù))決定了沿接觸面滑動(dòng)時(shí)的摩擦力大小
@property (readwrite, nonatomic) CGFloat density;//(密度) 跟size結(jié)合使用,計(jì)算物體的總質(zhì)量。質(zhì)量越大,物體加速或減速就越困難
@property (readwrite, nonatomic) CGFloat resistance;//(阻力):決定線性移動(dòng)的阻力大小,與摩擦系數(shù)不同,摩擦系數(shù)只作用于滑動(dòng)運(yùn)動(dòng)
@property (readwrite, nonatomic) CGFloat angularResistance;//(角阻力) :決定旋轉(zhuǎn)運(yùn)動(dòng)時(shí)的阻力大小
@property (readwrite, nonatomic) CGFloat charge;//
@property (nonatomic, getter = isAnchored) BOOL anchored;//
@property (readwrite, nonatomic) BOOL allowsRotation; //(允許旋轉(zhuǎn)):這個(gè)屬性很有意思,它在真實(shí)的物理世界沒(méi)有對(duì)應(yīng)的模型。設(shè)置這個(gè)屬性為 NO 物體就完全不會(huì)轉(zhuǎn)動(dòng),而無(wú)論施加多大的轉(zhuǎn)動(dòng)力
- (void)addLinearVelocity:(CGPoint)velocity forItem:(id <UIDynamicItem>)item;//
- (CGPoint)linearVelocityForItem:(id <UIDynamicItem>)item;//
- (void)addAngularVelocity:(CGFloat)velocity forItem:(id <UIDynamicItem>)item;//
- (CGFloat)angularVelocityForItem:(id <UIDynamicItem>)item;/