iOS_UIKit框架下的力學(xué)動畫

所有內(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 的二維坐標(biāo)系統(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 做的改變,相當(dāng)于代碼改變 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

// 當(dāng)該動態(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.gif

因為是第一次制作gif,所以看起來不是很流暢,一幀一幀的感覺,事實不是這個樣子的。

UICollisionBehavior 彈力類

指定一個邊界,當(dāng)物體接觸到一個邊界的時候,就會進行回彈。這個類室友一個代理可以通過監(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è)置了兩個物體的碰撞。

彈力gif.gif

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; //物體牽引點
}

非常好玩。。。(手動滑稽)


牽引力.gif

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];

這里推力就是從手指點擊屏幕的地方進行計算的。


推力.gif

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容