IOS中常用動畫方法總結

一.基本方式:使用UIView類的UIViewAnimation擴展

+ (void)beginAnimations:(NSString *)animationID context:(void *)context; // 開始準備動畫
+ (void)commitAnimations; // 運行動畫

// 沒有get方法,下面的set在快外調用無效
+ (void)setAnimationDelegate:(id)delegate; // 委託default = nil
+ (void)setAnimationWillStartSelector:(SEL)selector; // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
+ (void)setAnimationDidStopSelector:(SEL)selector; // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
+ (void)setAnimationDuration:(NSTimeInterval)duration; // default = 0.2動畫時間
+ (void)setAnimationDelay:(NSTimeInterval)delay; // default = 0.0延遲多少時間開始執行動畫
+ (void)setAnimationStartDate:(NSDate *)startDate; // default = now ([NSDate date])動畫開始日期?不知道啥用.- -
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; // default = UIViewAnimationCurveEaseInOut動畫方式
+ (void)setAnimationRepeatCount:(float)repeatCount; // default = 0.0. May be fractional重複次數
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; // default = NO. YES的話,動畫(非最後一次)結束後動態復原到最開始狀態
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. YES,停止之前的動畫,從現在這裏開始新動畫the current view position is always used for new animations -- allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default).

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache; // 添加動畫到view上,cache是YES的時候比較高效,但是動畫過程中不能更新界面上的內容,NO時每一幀都重新畫,可以實時更新
+ (void)setAnimationsEnabled:(BOOL)enabled; // 是否忽略一些動畫設置
+ (BOOL)areAnimationsEnabled;


一個動畫的代碼

[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLine
ar];
[UIView setAnimationDuration:
2.7];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
// operation>>>
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// end<<<<<<
[UIView commitAnimations];

其中transition取值範圍

typedef enum {
UIViewAnimationTransitio
nNone,
UIViewAnimationTransitio
nFlipFromLeft,
UIViewAnimationTransitio
nFlipFromRight,
UIViewAnimationTransitio
nCurlUp,
UIViewAnimationTransitio
nCurlDown,
} UIViewAnimationTransitio
n;

特點:基礎,使用方便,但是效果有限

二.block方式:使用UIView類的UIViewAnimationWithBlocks擴展

函數說明

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);//間隔,延遲,動畫參數(好像沒用?),界面更改塊,結束塊

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))

+ (
void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void(^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); //toView added to fromView.superview, fromView removed from its superview界面替換,這裏的options參數有效

舉例:

[UIView animateWithDuration:0.7 delay:0 options:0 animations:^(){
self.view.alpha =
0.2;
[self.view exchangeSubviewAtIndex:
1 withSubviewAtIndex:0];
self.view.alpha =
1;
} completion:^(BOOL finished)
{

}];

當areAnimationsEnabled爲NO時,上面不能動畫顯示

[UIView transitionFromView:lImage toView:mImage duration:0.7 options:options completion:^(BOOL finished)
{
if (finished) {
[self.view addSubview:lImage];
[self.view sendSubviewToBack:lImage];
[self.view sendSubviewToBack:mImage];
}
}];

options取值範圍

enum {
UIViewAnimationOptionLay
outSubviews = 1 << 0,
UIViewAnimationOptionAll
owUserInteraction = 1 << 1, // turn on user interaction while animating
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // start all views from current value, not initial value
UIViewAnimationOptionRepeat = 1 << 3, // repeat animation indefinitely
UIViewAnimationOptionAutoreverse = 1 << 4, // if repeat, run animation back and forth
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // ignore nested duration
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // ignore nested curve
UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only)
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // flip to/from hidden state instead of adding/removing

UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCur
veEaseOut = 2 << 16,
UIViewAnimationOptionCur
veLinear = 3 << 16,

UIViewAnimationOptionTra
nsitionNone = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTra
nsitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTra
nsitionCurlUp = 3 << 20,
UIViewAnimationOptionTra
nsitionCurlDown = 4 << 20,
UIViewAnimationOptionTra
nsitionCrossDissolve = 5 << 20,//ios5
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,//ios5
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,//ios5
};
typedef NSUInteger UIViewAnimationOptions;

特點:快捷方便,效果更多.可以如上示例1那樣實現界面個元素屬性漸進變化的動態展示

對於不太複雜的動畫,上面的寫法很精練,因爲只有一條語句,如果動畫太過複雜了,寫在這樣一條語句中就會顯得冗長了,對於代碼調試沒那麼方非常方便。
三:Core animation
core animation是以objc語言封裝的一套圖形渲染,投影以及動畫的庫的結合,使創建的用戶界面變得非常容易,通過以下方法:
1:使用簡單的編程方法實現高性能的合成
2:使用層對象創建複雜的用戶界面。
3:輕量型數據結構,能夠同時使幾百個層產生動畫。
4:不依賴於應用的主線程,動畫在單獨的線程裏運行
5:改進了應用程序性能。應用程序只需要重畫它變化的部分(局部刷新)。
6:靈活的佈局管理方式
2. 相關類
使用core animation,開發者不需要使用底層的API或者OpenGL就可以創建漂亮的動畫界面。
core animation類分成以下幾個:
1:提供顯示內容的層
2:動畫和時間類
3:佈局和約束類
4:將多個層分成幾個原子更新的執行類
2.1 層(layer)
層是core animation的基礎,視圖的一個實例,有一個CALayer實例作爲父層(superlayer)以及在這層上添加的所有子層,創建的層結構成爲layer tree
2.2動畫和時間類
隱式動畫
層的許多可視屬性的改變可以產生隱式的動畫效果,因爲這些屬性都默認與一個動畫相關聯。通過簡單地設置可視的屬性值,層會由當前值到被設置的值產生漸變的動畫效果。比如,設置層的隱藏屬性爲真,將觸發一個逐漸消失的動畫效果。

顯式動畫
通過創建一個動畫類和指定所需要的動畫效果,顯式的動畫並不改變層的屬性。
所有的核心動畫類都由抽象類CAAnimation繼承而來。CAAnimation採用CAMediaTiming協議。CAMediaTiming規定了動畫的持續時間,速度以及重複次數。CAAnimation也採用了CAAction協議,該協議規定了在響應由層觸發的動作時,開始一個動畫的標準方式。

核心動畫還提供其他的動畫類
CATransition,
CATransition規定了影響整個層內容的轉換效果,在動畫期間,它使層產生漸變(fade),推拉(push)以及揭示(reveal)的動畫效果。這些常用的轉換效果可以通過核心繪圖過濾器進行擴展。

CAAnimation,
CAAnimation允許大量的動畫對象被分爲幾組,並且可以同時運行。
CAPropertyAnimation,是CAAnimation的子類,支持層在動畫期間,爲層指定一個關鍵路徑。
CABasicAnimation.該類爲層的屬性提供了簡單的插值。
CAKeyframeAnimation,
CAKeyframeAnimation提供關鍵幀動畫的支持。你可以爲層的屬性指定一個關鍵路徑
2.3佈局管理類
CAKeyframeAnimation類用於管理所有子層的佈局,每個由CAConstraint類封裝的實例描述了各個子層之間的幾何位置關係。
2.4 執行管理類
可設置動畫層的屬性的修改必須是執行的一部分,CATransaction負責將許多動畫分成幾批原子型的更新進行顯示。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章