iOS動畫彙總

iOS 簡單動畫主要分爲三種(這是我的理解):

第一種:改變位置,大小等

  1. //開始動畫   
  2. [UIView beginAnimations:nil context:nil];    
  3. //設定動畫持續時間   
  4. [UIView setAnimationDuration:2];   
  5. //動畫的內容   
  6. frame.origin.x += 150;   
  7. [img setFrame:frame];   
  8. //動畫結束   
  9. [UIView commitAnimations];   


第二種:使用UIViewAnimationTransition


  1. [UIView beginAnimations:@"animation" context:nil];  
  2. [UIView setAnimationDuration:0.6f];  
  3. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  4. [UIView setAnimationDelegate:self];  
  5. [UIView setAnimationDidStopSelector:@selector(animationDidStop)];  
  6. [UIView setAnimationRepeatAutoreverses:NO];  
  7. [self.view addSubview:self.currentView];  
  8. [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigationController.view cache:YES];  
  9. [UIView commitAnimations];  

UIViewAnimationTransition動畫主要分爲兩種,
一種是掀開動畫(就像翻日曆),分別是 UIViewAnimationTransitionCurlUp , UIViewAnimationTransitionCurlDown .
另一種是翻轉動畫(一般地圖和列表切換會使用這種),分別是UIViewAnimationTransitionCurlUp,    UIViewAnimationTransitionCurlDown .

就是上圖的第一排4個按鈕展現的動畫

第三種:使用CATransition

  1. CATransition *transition = [CATransition animation];  
  2. transition.duration = 0.4;  
  3. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
  4. transition.type = kCATransitionPush;  
  5. transition.subtype = kCATransitionFromRight;  
  6. [fromView.layer addAnimation:transition forKey:nil];  

CATransition動畫分爲4種,

1、kCATransitionFade;

2、kCATransitionPush;

3、kCATransitionReveal;

4、kCATransitionMoveIn;

後三種又可以選擇4個方向,kCATransitionFromLeft 、kCATransitionFromBottom 、kCATransitionFromRight 、kCATransitionFromTop

就是上圖的第2排4個按鈕展現的動畫



從iOS4.0以後,apple又提供了5個方法:
  1. + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);  
  2.   
  3. + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0  
  4.   
  5. + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL  
  6.   
  7. + (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);  
  8.   
  9. + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // toView added to fromView.superview, fromView removed from its superview  

例如:改變大小位置
  1. [UIView animateWithDuration:0.6 animations:^{  
  2.         self.currentView.frame = CGRectMake(30, 30, 200, 300);  
  3.     }];  
或者翻轉切換視圖
  1. [UIView transitionFromView:self.mapviewContainer toView:self.listContainer duration:0.75 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished){  
  2.           
  3.     }];  

下面8個是私有api,如果應用要上appstore,請勿使用。
就是上圖的下面8個按鈕展現的動畫



發佈了4 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章