1. 程式人生 > >IOS動畫簡介

IOS動畫簡介

四.UIView動畫(指頁面切換過渡效果的動畫)

基本方式UIView類的擴充套件類UIViewAnimation

發出beginAnimations:context:請求標誌著動畫塊的開始;commitAnimations標誌著動畫塊的結束。
把這兩個類方法傳送給UIView而不是傳送給單獨的檢視。在這兩個呼叫之間的可定義動畫的展現方式並更新檢視。
函式說明:
//開始準備動畫
+ (void)beginAnimations:(NSString *)animationID context:(void *)context;

//執行動畫
+ (void)commitAnimations;
[UIView beginAnimations:nil context:nil];

  //setAnimationCurve來定義動畫加速或減速方式
 [UIView setAnimaitonCurve:UIViewAnimationCurveLinear]; 
  [UIView setAnimationDuration:時長]; //動畫時長
 [UIView setAnimationTransition:翻轉方向 forView:self.view cache:YES];
 [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];//切換檢視
  [UIView commitAnimations];//結束動畫

CGContextRef context = UIGraphicsGetCurrentContext(); //返回當前檢視堆疊頂部的圖形上下文

 [UIView beginAnimations:nil context:context];
 [UIView setAnimaitonCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[contextView setAlpha:0.0f];
[UIView commitAnimations];
/*
UIView類本身提供四種過渡翻轉效果
UIViewAnimationTransitionNone 正常
UIViewAnimationTransitionFlipFromLeft 從左向右翻
UIViewAnimationTransitionFlipFromRight 從右向左翻

UIViewAnimationTransitionCurlUp 從下向上卷
UIViewAnimationTransitionCurlDown 從上向下卷 
*/


core方式:使用CATransition類
 iPhone還支援Core Animation作為其QuartzCore架構的一部分,CA API為iPhone應用程式提供了高度靈活的動畫解決方案。但是須知:CATransition只針對圖層,不針對檢視。
 圖層是Core Animation與每個UIView產生聯絡的工作層面。使用Core Animation時,應如果食用CATransition應該應用到檢視的預設圖層 (layer)而不是檢視(View)本身。
 使用CATransition類實現動畫,只需要建立一個Core Animation物件,設定它的引數,然後把這個帶引數的過渡新增到圖層即可。
  CATransition *transition = [CATransition animation];//初始化
  transition.duration = 0.7;//動畫時間
  transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  transition.type = kCATransitionMoveIn;//動畫過渡種類(淡化、推擠、揭開、覆蓋)
/*動畫型別
  kCATransitionMoveIn,
  kCATransitionPush,
    kCATransitionReveal, 
  kCATransitionFade
*/

  //更多私有{@"cube",@"suckEffect",@"oglFlip",@"rippleEffect",@"pageCurl",@"pageUnCurl",@"cameraIrisHollowOpen",@"cameraIrisHollowClose"};
   動畫型別有(立方體、吸收、翻轉、波紋、翻頁、反翻頁、鏡頭開、鏡頭關)。
  transition.subtype = kCATransitionFromLeft;//翻轉方向
/*翻轉方向
kCATransitionFromLeft,
  kCATransitionFromRight,
  kCATransitionFromTop,
  kCATransitionFromBottom
   */
   transition.delegate = self;//設定代理
   [self.view.layer addAnimation:transition forKey:nil];//動畫必須新增到layer層
   [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];//實際要做的內容


block方式: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 __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); 
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);
+ (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);