所有內(nèi)容都是學(xué)習(xí)自在網(wǎng)上分享的文章
Dynamic
iOS7中推出的UIKit Dynamics备图,主要用于模擬現(xiàn)實中的二維動畫诬烹。
主要類
UIDynamicAnimator: 封裝了底層iOS物理引擎,為動力項
UIDynamicBehavioer: 動力行為幢痘,為動力項提供不同的動力行為
UIDynamicItem: 一個物體颜说,被各種力量作用的物體
UIDynamicAnimatior
@property (nonatomic, strong) UIDynamicAnimator *dynamic;
// 初始化動畫的持有者 //ReferenceView是參考視圖门粪,就是以他為相對位置
_dynamic = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
// 傳入一個 Reference view 創(chuàng)建一個 Dynamic Animator
- (instancetype)initWithReferenceView:(UIView*)view;
// 獲取在 CGRect 內(nèi)所有的動力項玄妈,這個 CGRect 是基于 Reference view 的二維坐標系統(tǒng)的
- (NSArray*)itemsInRect:(CGRect)rect;
// 添加動力行為
- (void)addBehavior:(UIDynamicBehavior *)behavior;
// 刪除指定的動力行為
- (void)removeBehavior:(UIDynamicBehavior *)behavior;
// 刪除所有的動力行為
- (void)removeAllBehaviors;
///Dynamic Animator‘s狀態(tài)
@property (nonatomic, readonly, getter = isRunning) BOOL running;
// 獲取所有的 Behaviors
@property (nonatomic, readonly, copy) NSArray* behaviors;
@property (nonatomic, readonly) UIView* referenceView;
// 這個 delegate 中有兩個回調(diào)方法拟蜻,一個是在 animator 暫停的時候調(diào)用酝锅,一個是在將要恢復(fù)的時候調(diào)用
@property (nonatomic, assign) id <UIDynamicAnimatorDelegate> delegate;
// 已經(jīng)運行了多久的時間搔扁,是一個 NSTimeInterval
- (NSTimeInterval)elapsedTime;
// 如果動力項不是通過 animator 自動計算改變狀態(tài)稿蹲,比如场绿,通過代碼強制改變一個 item 的 transfrom 時焰盗,可以用這個方法通知 animator 這個 item 的改變熬拒。如果不用這個方法澎粟,animator 之后的動畫會覆蓋代碼中對 item 做的改變欢瞪,相當于代碼改變 transform 變得沒有意義遣鼓。
- (void)updateItemUsingCurrentState:(id <UIDynamicItem>)item;
UIDynamicBehavior
UIDynamicBehavior是所有物理行為的父類
// 在將要進行動畫時的 block 回調(diào)
@property(nonatomic, copy) void (^action)(void)
// 添加到該動態(tài)行為中的子動態(tài)行為
@property(nonatomic, readonly, copy) NSArray *childBehaviors
// 該動態(tài)行為相關(guān)聯(lián)的dynamicAnimator
@property(nonatomic, readonly) UIDynamicAnimator *dynamicAnimator
//添加一個子動態(tài)行為
- (void)addChildBehavior:(UIDynamicBehavior *)behavior
// 移除一個子動態(tài)行為
- (void)removeChildBehavior:(UIDynamicBehavior *)behavior
// 當該動態(tài)行為將要被添加到一個UIDynamicAnimator中時回懦,這個方法會被調(diào)用次企。
- (void)willMoveToAnimator:(UIDynamicAnimator *)dynamicAnimator
UIGravityBehavior重力類
UIGravityBehavior:重力行為需要指定重力的大小和方向缸棵,用gravityDircetion指定一個向量堵第,或者設(shè)置angle和magnitude型诚。
angle:0 - 3.14 是一個從平衡向左到向右的方向狰贯。
magnitude:是重力系數(shù)涵紊。通常設(shè)置angle和magnitide更加的直觀
因為是第一次制作gif摸柄,所以看起來不是很流暢驱负,一幀一幀的感覺,事實不是這個樣子的苛吱。
UICollisionBehavior 彈力類
指定一個邊界翠储,當物體接觸到一個邊界的時候援所,就會進行回彈任斋。這個類室友一個代理可以通過監(jiān)控物體和邊界物體和物體之間的碰撞時刻废酷。
//初始化彈力行為
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.animationView, self.greView]];
//設(shè)置邊界
//[collisionBehavior addBoundaryWithIdentifier:@"邊界" fromPoint:CGPointMake(0, 0) toPoint:CGPointMake(self.view.frame.size.height, 600)];
/**
UICollisionBehaviorModeItems = 1 << 0, 物體間碰撞
UICollisionBehaviorModeBoundaries = 1 << 1, 邊界碰撞
UICollisionBehaviorModeEverything = NSUIntegerMax
*/
//是否把關(guān)聯(lián)視圖設(shè)置為邊界澈蟆,這里的關(guān)聯(lián)視圖指的就是UIDynamicAnimator中的視圖趴俘。把該屬性設(shè)置為YES寥闪,運行代碼,大家會發(fā)view掉落到底部時會與底部放生彈性碰撞缚柳。//前提條件是不超過所設(shè)邊界秋忙。 如果邊界小的話只是在邊界內(nèi)活動
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
//代理
collisionBehavior.collisionDelegate = self;
[_dynamic addBehavior:collisionBehavior];
//與物體碰撞時執(zhí)行的代理方法
- (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
//與邊界碰撞時執(zhí)行的代理方法
- (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;
我把石頭設(shè)置了重力刽肠,并且設(shè)置了兩個物體的碰撞。
UIAttachmentBehavior 附著行為、吸附力
對于這個類凯肋,網(wǎng)上的名稱有很多,看到一段解釋:關(guān)于吸附力姚建,首先要解釋一下流礁,大家可以把吸附力理解為在吸附原點有一根棍萎坷,注意是棍不是繩子哆档,連接著item瓜浸。也就是說吸附力是剛性的插佛。這里我更加偏向于這是一個類似彈簧的力雇寇。
/**
@property (readonly, nonatomic) UIAttachmentBehaviorType attachedBehaviorType; 模式 @property (readwrite, nonatomic) CGPoint anchorPoint; //吸附點
*/
_attachment = [[UIAttachmentBehavior alloc] initWithItem:self.animationView attachedToAnchor:CGPointMake(self.view.frame.size.width / 2, 0)];
_attachment.length = 100; //吸附距離
_attachment.damping = 0.5; //阻尼 // 那個棍子吸附的力量,越大就越不能伸縮 冬殃,類似于一個跟300xp的彈簧。
_attachment.frequency = 1; //振動頻率
[_dynamic addBehavior:_attachment];
并且在手指在屏幕移動的方法中叁怪,將anchorPoint跟隨手指移動,并且保持了彈力审葬。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// 獲取在屏幕上的手指對象
UITouch *touch = [touches anyObject];
// 獲取手指之前在屏幕上的位置
CGPoint previousP = [touch previousLocationInView:self.view];
// 獲取現(xiàn)在的位置
CGPoint currentP = [touch locationInView:self.view];
CGPoint newP = CGPointMake(0, 0);
newP.x = self.animationView.center.x + (currentP.x - previousP.x);
newP.y = self.animationView.center.y + (currentP.y - previousP.y);
// self.animationView.center = newP;
// self.greView.center = currentP;
self.attachment.anchorPoint = currentP; //物體牽引點
}
非常好玩。奕谭。涣觉。(手動滑稽)
UIPushBehavior 推力
對物體施加的力,可以是持續(xù)的力也可以是一次性的理展箱,有一個向量來表示里的方向和大小蹬昌。
UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[self.animationView] mode:(UIPushBehaviorModeInstantaneous)];
/**
* 模式:UIPushBehaviorModeContinuous 持續(xù)型
* UIPushBehaviorModeInstantaneous 一次性
*/
//推力速度
push.magnitude = 2;
//推的方向
push.angle = 2;
[_dynamic addBehavior:push];
這是屬性值跟重力的屬性值其實是一樣的混驰,也可以用向量的方式來定義。
UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[self.animationView] mode:(UIPushBehaviorModeInstantaneous)];
// //推力速度
// push.magnitude = 2;
// //推的方向
// push.angle = 2;
push.pushDirection = CGVectorMake(currentP.x - self.animationView.center.x, currentP.y - self.animationView.center.y);
[_dynamic addBehavior:push];
這里推力就是從手指點擊屏幕的地方進行計算的皂贩。
UISnapBehavior 黑洞 吸力
將物體向一個點吸
UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.animationView snapToPoint:CGPointMake(100, 100)];
snap.damping = 50; //阻尼
[self.dynamic addBehavior:snap];
這個類只有兩個屬性
snapPoint 黑洞的點
damping 阻尼 vaule 0...1之間
UIDynamicItemBehavior 自身物體屬性
由于所有內(nèi)容都是股溝前輩們的文章學(xué)習(xí)的栖榨,但是這個類好多博客都沒有詳細介紹或或者被末學(xué)忽略了,在不經(jīng)意瀏覽中看到了這個大神的博客明刷。風(fēng)格排版非常喜歡婴栽,內(nèi)容也很有深度,在此做記錄辈末,以后多想前輩學(xué)習(xí)愚争。
大神的博客鏈接
在這個類中可以添加物體屬性,如密度挤聘、彈性系數(shù)轰枝、摩擦系數(shù)、阻力组去、轉(zhuǎn)動阻力等鞍陨。
/**
1、@property (readwrite, nonatomic) CGFloat elasticity; 0...1 彈性系數(shù)
2从隆、@property (readwrite, nonatomic) CGFloat friction; // 摩擦系數(shù)诚撵,0就是絕對光滑
3、@property (readwrite, nonatomic) CGFloat density; // 密度1 by default
4键闺、@property (readwrite, nonatomic) CGFloat resistance; // 0: no velocity damping 運動過程中受到的阻力
5寿烟、@property (readwrite, nonatomic) CGFloat angularResistance; // 0: no angular velocity damping
旋轉(zhuǎn)時受到的阻力
6、@property (readwrite, nonatomic) BOOL allowsRotation; //允許自身旋轉(zhuǎn)
*/
UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.animationView]];
itemBehavior.elasticity = 0.8; //自身彈性
itemBehavior.allowsRotation = YES; //允許旋轉(zhuǎn)
[itemBehavior addAngularVelocity:1 forItem:self.animationView]; //讓物體旋轉(zhuǎn)
[self.dynamic addBehavior:itemBehavior];
寫的非常垃圾的代碼https://github.com/kyrielrvingcoding/UIDynamics.git