iOS動畫效果和實現

來源:互聯網
上載者:User

標籤:

動畫效果提供了狀態或頁面轉換時流暢的使用者體驗,在iOS系統中,咱們不需要自己編寫繪製動畫的代碼,Core Animation提供了豐富的api來實現你需要的動畫效果。

    UIKit只用UIView來展示動畫,動畫支援UIView下面的這些屬性改變:

 

  • frame  
  • bounds  
  • center  
  • transform  
  • alpha 
  • backgroundColor 
  • contentStretch
 1、commitAnimations方式使用UIView動畫 [cpp] view plain copy 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  6.       
  7.     [button setTitle:@"改變" forState:UIControlStateNormal];  
  8.     button.frame = CGRectMake(10, 10, 60, 40);  
  9.     [button addTarget:self action:@selector(changeUIView) forControlEvents:UIControlEventTouchUpInside];  
  10.     [self.view addSubview:button];  
  11.       
  12. }  
  13.   
  14. - (void)changeUIView{  
  15.     [UIView beginAnimations:@"animation" context:nil];  
  16.     [UIView setAnimationDuration:1.0f];  
  17.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  18.     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];  
  19.     [UIView commitAnimations];  
  20.  }  

下面是點擊改變後的效果(兩種): 動畫的常量有一下四種 [cpp] view plain copy 
  1. UIViewAnimationTransitionNone,  
  2. UIViewAnimationTransitionFlipFromLeft,  
  3. UIViewAnimationTransitionFlipFromRight,  
  4. UIViewAnimationTransitionCurlUp,  
  5. UIViewAnimationTransitionCurlDown,  

1.2 交換本視圖控制器中2個view位置

 [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];

先添加兩個view ,一個redview  一個yellowview

 

[cpp] view plain copy 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     UIView *redView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  5.     redView.backgroundColor = [UIColor redColor];  
  6.     [self.view addSubview:redView];  
  7.       
  8.     UIView *yellowView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  9.     yellowView.backgroundColor = [UIColor yellowColor];  
  10.     [self.view addSubview:yellowView];  
  11.       
  12.     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  13.     [button setTitle:@"改變" forState:UIControlStateNormal];  
  14.     button.frame = CGRectMake(10, 10, 300, 40);  
  15.     [button addTarget:self action:@selector(changeUIView) forControlEvents:UIControlEventTouchUpInside];  
  16.     [self.view addSubview:button];  
  17.       
  18.     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  19.     [button1 setTitle:@"改變1" forState:UIControlStateNormal];  
  20.     button1.frame = CGRectMake(10, 60, 300, 40);  
  21.     [button1 addTarget:self action:@selector(changeUIView1) forControlEvents:UIControlEventTouchUpInside];  
  22.     [self.view addSubview:button1];  
  23.       
  24. }  

[cpp] view plain copy 
  1. - (void)changeUIView1{  
  2.     [UIView beginAnimations:@"animation" context:nil];  
  3.     [UIView setAnimationDuration:1.0f];  
  4.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  5.     [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];  
  6.     //  交換本視圖控制器中2個view位置  
  7.     [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];  
  8.     [UIView commitAnimations];  
  9. }  

 

這樣看起來就像兩頁一樣了。1.3 、   [UIView setAnimationDidStopSelector:@selector(animationFinish:)];

在commitAnimations訊息之前,可以設定動畫完成後的回調,設定方法是:

    [UIView setAnimationDidStopSelector:@selector(animationFinish:)];

 2、使用:CATransition [cpp] view plain copy 
  1. - (void)changeUIView2{  
  2.     CATransition *transition = [CATransition animation];  
  3.     transition.duration = 2.0f;  
  4.       transition.type = kCATransitionPush;  
  5.     transition.subtype = kCATransitionFromTop;  
  6.     [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];  
  7.     [self.view.layer addAnimation:transition forKey:@"animation"];  
  8. }  
transition.type 的類型可以有

淡化、推擠、揭開、覆蓋

NSString * const kCATransitionFade;

NSString * const kCATransitionMoveIn;

NSString * const kCATransitionPush;

NSString * const kCATransitionReveal;

這四種,transition.subtype 也有四種

NSString * const kCATransitionFromRight;

NSString * const kCATransitionFromLeft;

NSString * const kCATransitionFromTop;

NSString * const kCATransitionFromBottom;


 2.2 私人的類型的動畫類型:

立方體、吸收、翻轉、波紋、翻頁、反翻頁、鏡頭開、鏡頭關

[cpp] view plain copy 
  1. animation.type = @"cube"  
  2. animation.type = @"suckEffect";    
  3. animation.type = @"oglFlip";//不管subType is "fromLeft" or "fromRight",official只有一種效果  
  4. animation.type = @"rippleEffect";   
  5. animation.type = @"pageCurl";   
  6. animation.type = @"pageUnCurl"  
  7. animation.type = @"cameraIrisHollowOpen ";  
  8. animation.type = @"cameraIrisHollowClose ";  
是第一個cube立方體的效果:2.3 CATransition的 startProgress  endProgress屬性這兩個屬性是float類型的。
可以控制動畫進行的過程,可以讓動畫停留在某個動畫點上,值在0.0到1.0之間。endProgress要大於等於startProgress。比如上面的立方體轉到,可以設定endProgress= 0.5,讓動畫停留在轉動一般的位置。上面這些私人的動畫效果,在實際應用中要謹慎使用。因為在app store審核時可能會以為這些動畫效果而拒絕通過。  3、UIView的 + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion方法。這個方法是在iOS4.0之後才支援的。比 1 裡的UIView的方法簡潔方便使用。DidView裡添加moveView。 [cpp] view plain copy 
  1. moveView = [[UIView alloc] initWithFrame:CGRectMake(10, 180, 200, 40)];  
  2. moveView.backgroundColor = [UIColor blackColor];  
  3. [self.view addSubview:moveView];  

 

[cpp] view plain copy 
  1. - (void)changeUIView3{  
  2.     [UIView animateWithDuration:3 animations:^(void){  
  3.         moveView.frame = CGRectMake(10, 270, 200, 40);  
  4.     }completion:^(BOOL finished){  
  5.         UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 40, 40)];  
  6.         label.backgroundColor = [UIColor blackColor];  
  7.         [self.view addSubview:label];  
  8.     }];  
  9. }  
然後用UIView animateWithDuration動畫移動,移動動畫完畢後添加一個Label。

 

3.2、 animateWithDuration的嵌套使用 [cpp] view plain copy 
  1. - (void)changeUIView3{  
  2.    
  3.       
  4.     [UIView animateWithDuration:2  
  5.                           delay:0  
  6.                         options:UIViewAnimationOptionCurveEaseOut animations:^(void){  
  7.         moveView.alpha = 0.0;  
  8.     }completion:^(BOOL finished){  
  9.         [UIView animateWithDuration:1  
  10.                               delay:1.0  
  11.                             options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat  
  12.                          animations:^(void){  
  13.                              [UIView setAnimationRepeatCount:2.5];  
  14.                              moveView.alpha = 1.0;  
  15.                          }completion:^(BOOL finished){  
  16.                                
  17.                          }];  
  18.           
  19.     }];  
  20. }  
這個嵌套的效果是先把view變成透明,在從透明變成不透明,重複2.5次透明到不透明的效果。文中例子的代碼:AnimateDemo

iOS動畫效果和實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.