二.block方式:使用UIView類的UIViewAnimationWithBlocks擴展 ios
函數說明 編程
+ (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.7delay:0options:0animations:^(){
self.view.alpha = 0.2;
[self.view exchangeSubviewAtIndex:1withSubviewAtIndex:0];
self.view.alpha = 1;
} completion:^(BOOL finished)
{
}];
當areAnimationsEnabled爲NO時,上面不能動畫顯示 app
[UIView transitionFromView:lImage toView:mImage duration:0.7options:options completion:^(BOOL finished)
{
if(finished) {
[self.view addSubview:lImage];
[self.view sendSubviewToBack:lImage];
[self.view sendSubviewToBack:mImage];
}
}];
options取值範圍 ide
enum{
UIViewAnimationOptionLayoutSubviews = 1<< 0,
UIViewAnimationOptionAllowUserInteraction = 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,
UIViewAnimationOptionCurveEaseOut = 2<< 16,
UIViewAnimationOptionCurveLinear = 3<< 16,
UIViewAnimationOptionTransitionNone = 0<< 20, //default
UIViewAnimationOptionTransitionFlipFromLeft = 1<< 20,
UIViewAnimationOptionTransitionFlipFromRight = 2<< 20,
UIViewAnimationOptionTransitionCurlUp = 3<< 20,
UIViewAnimationOptionTransitionCurlDown = 4<< 20,
UIViewAnimationOptionTransitionCrossDissolve = 5<< 20,//ios5
UIViewAnimationOptionTransitionFlipFromTop = 6<< 20,//ios5
UIViewAnimationOptionTransitionFlipFromBottom = 7<< 20,//ios5
};
typedef NSUInteger UIViewAnimationOptions;
特色:快捷方便,效果更多.能夠如上示例1那樣實現界面個元素屬性漸進變化的動態展現 函數
對於不太複雜的動畫,上面的寫法很精練,由於只有一條語句,若是動畫太過複雜了,寫在這樣一條語句中就會顯得冗長了,對於代碼調試沒那麼方很是方便。
三:Core animation
coreanimation是以objc語言封裝的一套圖形渲染,投影以及動畫的庫的結合,使建立的用戶界面變得很是容易,經過如下方法:
1:使用簡單的編程方法實現高性能的合成
2:使用層對象建立複雜的用戶界面。
3:輕量型數據結構,可以同時使幾百個層產生動畫。
4:不依賴於應用的主線程,動畫在單獨的線程裏運行
5:改進了應用程序性能。應用程序只須要重畫它變化的部分(局部刷新)。
6:靈活的佈局管理方式
2. 相關類
使用core animation,開發者不須要使用底層的API或者OpenGL就能夠建立漂亮的動畫界面。
core animation類分紅如下幾個:
1:提供顯示內容的層
2:動畫和時間類
3:佈局和約束類
4:將多個層分紅幾個原子更新的執行類
2.1 層(layer)
層是coreanimation的基礎,視圖的一個實例,有一個CALayer實例做爲父層(superlayer)以及在這層上添加的全部子層,建立的層結構成爲layertree
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負責將許多動畫分紅幾批原子型的更新進行顯示。