iOS 動畫篇: UIView Animation

UIVIew Animation 是 iOS 提供的最基礎(chǔ)的一組用于實現(xiàn) UIView 動畫的類庫。在 UIView Animation 中,可以改變的屬性有:

  • frame
  • bounds
  • center
  • alpha
  • transform

UIView 類提供了大量的動畫 API ,這些 API 集中在三個 Category 里,分別是:

  1. UIView (UIViewAnimation) - basic animation 基礎(chǔ)動畫
  2. UIView (UIViewAnimationWithBlocks) - basic animation 基礎(chǔ)動畫
  3. UIView (UIViewKeyframeAnimations) - keyframe animation 關(guān)鍵幀動畫

1. UIViewAnimation

UIViewAnimation 誕生時間最早,功能上完全可以使用其余兩個 Cagetory 替代,其中方法包含:

+ (void)beginAnimations:(nullable NSString )animationID context:(nullable void )context;  
+ (void)commitAnimations;    
+ (void)setAnimationDelegate:(nullable id)delegate;           
+ (void)setAnimationWillStartSelector:(nullable SEL)selector;  
+ (void)setAnimationDidStopSelector:(nullable SEL)selector;  
+ (void)setAnimationDuration:(NSTimeInterval)duration;  
+ (void)setAnimationDelay:(NSTimeInterval)delay;                   
+ (void)setAnimationStartDate:(NSDate )startDate;  
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve;  
+ (void)setAnimationRepeatCount:( float)repeatCount;  
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;  
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState;   
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView )view cache:(BOOL)cache;    
+ (void)setAnimationsEnabled:(BOOL)enabled;  
+ (BOOL)areAnimationsEnabled; 
//阻塞動畫,iOS 7 添加的新方法[UIView performWithoutAnimation:]。它是一個簡單的封裝,先檢查動畫當(dāng)前是否啟用,然后禁止動畫,執(zhí)行塊語句,最后重新啟用動畫。需要說明的地方是,它并不會阻塞基于CoreAnimation的動畫。 
+ (void)performWithoutAnimation:(void (^)(void))actionsWithoutAnimation   
+ (NSTimeInterval)inheritedAnimationDuration 

方法比較簡單,使用時先 beginAnimations(傳入的 animationID 作為該動畫的標(biāo)識,可以在 delegate 中清楚的識別到該動畫),然后設(shè)置動畫的各項屬性,如 duration, delegate 等,設(shè)置完成后 commitAnimations。

 - (void) startAnimation {
 
    [UIView beginAnimations:@"UIViewAnimation" context:(__bridge void *)(self)];
    
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelay:0.0];
    
    [UIView setAnimationRepeatCount:2];
    [UIView setAnimationRepeatAutoreverses:YES];
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    
    _animationView.center = CGPointMake(CGRectGetMaxX(self.view.bounds) - 25, CGRectGetMidY(self.view.bounds));
    
    [UIView commitAnimations];
}

需要說明的是,UIViewAnimationCurve 表示動畫的變化規(guī)律:

  • UIViewAnimationCurveEaseInOut: 開始和結(jié)束時較慢
  • UIViewAnimationCurveEase: 開始時較慢
  • UIViewAnimationCurveEaseOut: 結(jié)束時較慢
  • UIViewAnimationCurveLinear: 整個過程勻速進行
    具體效果可參考下圖:
**UIViewAnimationCurve**

2. UIViewAnimationWithBlocks

UIViewAnimationWithBlocks 是在 iOS 4.0 時推出的基于 block 的 Animation Category,較 UIViewAnimation 來說,使用起來更加便捷。

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ nullable)(BOOL finished))completion ;  
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ nullable)(BOOL finished))completion ;  
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations ;   
+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray<kindof UIView > )views options:(UIViewAnimationOptions)options animations:(void (^ nullable)(void))parallelAnimations completion:(void (^ nullable)(BOOL finished))completion ;

這是 UIViewAnimationWithBlocks 中最常用的三種方法,使用 block 方式實現(xiàn)基本動畫,可以設(shè)置 duration持續(xù)時間,delay延遲時間,UIViewAnimationOptions枚舉項和completion動畫結(jié)束的回調(diào)。

時間函數(shù)

動畫的速度曲線是由時間函數(shù)( timing function )控制的。

彈簧動畫

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ nullable)(BOOL finished))completion   

iOS 7.0 增加的方法,新添了兩個參數(shù),springDampinginitialSpringVelocity。

  • springDamping:彈性阻尼,取值范圍時 0 到 1,越接近 0 ,動畫的彈性效果就越明顯;如果設(shè)置為 1,則動畫不會有彈性效果。
  • initialSpringVelocity:視圖在動畫開始時的速度,>= 0。
damping

在 initialSpringVelocity 為 0 ,damping 分別為 0.4,0.6,0.8 的情況下效果如上圖,可見阻尼越小,彈簧效果越明顯。

initialSpringVelocity

在 damping 為 1 ,initialSpringVelocity 分別為 0,5,30 的情況下效果如上圖,可見初始速度越大彈簧效果越明顯,彈動的幅度越大。

+ (void)transitionWithView:(UIView )view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ nullable)(void))animations completion:(void (^ nullable)(BOOL finished))completion ;  
+ (void)transitionFromView:(UIView )fromView toView:(UIView )toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^ nullable)(BOOL finished))completion ;  

3. UIViewKeyframeAnimations

UIViewAnimationWithBlocks 推出于 iOS 7.0,用來實現(xiàn)幀動畫 ?;A(chǔ)動畫只能將 UIView的屬性從一個值變化到另一個值,而關(guān)鍵幀動畫可以包含任意一個關(guān)鍵幀,使 UIView在多個值之間進行變化。關(guān)鍵幀動畫可以看成是若干個連續(xù)執(zhí)行的基礎(chǔ)動畫。

+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ nullable)(BOOL finished))completion ;  
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations ;

其中animateKeyframesWithDuration:delay:options:animations:completion:參數(shù)的使用方法與基礎(chǔ)動畫大致相同,只是基礎(chǔ)動畫的 options 是 UIViewAnimationOptions 類型,而關(guān)鍵幀動畫的 options 是 UIViewKeyAnimationOptions 類型。另外,關(guān)鍵幀動畫的持續(xù)時間( duration )是整個動畫的持續(xù)時間,也就是所有關(guān)鍵幀持續(xù)時間的總和。
addKeyframeWithRelativeStartTime:relativeDuration:animations:中的第一個參數(shù)( relativeStartTime )是相對起始時間,表示該關(guān)鍵幀開始執(zhí)行的時刻在整個動畫持續(xù)時間中的百分比,取值范圍是[0-1]。第二個參數(shù)( relativeDuration )是相對持續(xù)時間,表示該關(guān)鍵幀占整個動畫持續(xù)時間的百分比,取值范圍也是[0-1]。

最后編輯于
?著作權(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)容

  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,688評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,260評論 5 13
  • 在iOS實際開發(fā)中常用的動畫無非是以下四種:UIView動畫,核心動畫,幀動畫,自定義轉(zhuǎn)場動畫。 1.UIView...
    請叫我周小帥閱讀 3,313評論 1 23
  • 概覽 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺iOS動畫全貌。在這里你...
    Yiart閱讀 3,960評論 3 34
  • 書寫的很好,翻譯的也棒!感謝譯者,感謝感謝! iOS-Core-Animation-Advanced-Techni...
    錢噓噓閱讀 2,428評論 0 6

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