CoreAnimation動畫

在IOS中若是使用普通的動畫則可使用UIKit提供的動畫方式來實現,若是想實現更復雜的效果,則須要使用Core Animation了。數組

下面詳解各類類型動畫的使用方式app

一、經過動畫上下文使用UIKit動畫

[plain] view plaincopy動畫

  1. -(void)animationOfUIKit  spa

  2. {  .net

  3.     UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];  orm

  4.     redView.backgroundColor=[UIColor redColor];  對象

  5.       

  6.     [self.view addSubview:redView];  blog

  7.     //開始動畫  ip

  8.     [UIView beginAnimations:@"test" context:nil];  get

  9.     //動畫時長  

  10.     [UIView setAnimationDuration:1];  

  11.     /*  

  12.      *要進行動畫設置的地方  

  13.      */  

  14.       

  15.     redView.backgroundColor=[UIColor blueColor];  

  16.     redView.frame=CGRectMake(50, 50, 200, 200);  

  17.     redView.alpha=0.5;  

  18.       

  19.       

  20.     //動畫結束  

  21.     [UIView commitAnimations];  

  22. }  


二、經過代碼塊使用UIKit動畫

[plain] view plaincopy

  1. -(void)animationOfBlock  

  2. {  

  3.     //初始化一個View,用來顯示動畫  

  4.     UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];  

  5.     redView.backgroundColor=[UIColor redColor];  

  6.       

  7.     [self.view addSubview:redView];  

  8.   

  9.     [UIView animateWithDuration:1 //時長  

  10.                           delay:0 //延遲時間  

  11.                         options:UIViewAnimationOptionTransitionFlipFromLeft//動畫效果  

  12.                      animations:^{  

  13.                            

  14.                          //動畫設置區域  

  15.                          redView.backgroundColor=[UIColor blueColor];  

  16.                          redView.frame=CGRectMake(50, 50, 200, 200);  

  17.                          redView.alpha=0.5;  

  18.                            

  19.                      } completion:^(BOOL finish){  

  20.                        //動畫結束時調用  

  21.                        //............  

  22.                      }];  

  23.       

  24.       

  25. }  


使用Core Animation對象來實現動畫


在Core Animation中咱們常常使用的是

  • CABasicAnimation

  • CAKeyframeAnimation

  • CATransitionAnimation

其中CABasicAnimationCAKeyframeAnimation是對圖層中的不一樣屬性進行動畫的。

若是要多整個圖層進行動畫,則應該使用CATransitionAnimation

若是要使用組合動畫,例如要改變圖層的大小和透明度,則能夠先爲每一個屬性建立一個CABasicAnimation對象,再把他們組合到CAAnimationGroup中,最後把這個組合添加到要進行動畫的CALayer中。

注:CAAnimation(以及CAAnimation的子類),所有都是顯式動畫,這樣動畫播放後,表現層回恢復到模型層的原始狀態,這就意味着,若是動畫播放完後,會恢復到原來的樣子,因此在動畫播放完後要對模型層進行修改,例如:self.view.layer.backgroundColor=[UIColor blueColor].CGColor;

一、自定義動畫:CABasicAnimation

[plain] view plaincopy

  1. -(void)animationOfCABasicAnimation  

  2. {  

  3.     //建立一個CABasicAnimation對象  

  4.     CABasicAnimation *animation=[CABasicAnimation animation];  

  5.     //設置顏色  

  6.     animation.toValue=(id)[UIColor blueColor].CGColor;  

  7.     //動畫時間  

  8.     animation.duration=1;  

  9.     //是否反轉變爲原來的屬性值  

  10.     animation.autoreverses=YES;  

  11.     //把animation添加到圖層的layer中,即可以播放動畫了。forKey指定要應用此動畫的屬性  

  12.     [self.view.layer addAnimation:animation forKey:@"backgroundColor"];  

  13.       

  14. }  


二、關鍵幀動畫:CAKeyframeAnimation

1. path

這是一個 CGPathRef  對象,默認是空的,當咱們建立好CAKeyframeAnimation的實例的時候,能夠經過制定一個本身定義的path來讓  某一個物體按照這個路徑進行動畫。這個值默認是nil  當其被設定的時候  values  這個屬性就被覆蓋 

2. values

一個數組,提供了一組關鍵幀的值,  當使用path的 時候 values的值自動被忽略。

下面是改變依次改變view的顏色

[plain] view plaincopy

  1. -(void)animationOfCAKeyframeAnimation  

  2. {  

  3.     CAKeyframeAnimation *animation=[CAKeyframeAnimation animation];  

  4.     //設置屬性值  

  5.     animation.values=[NSArray arrayWithObjects:  

  6.                       (id)self.view.backgroundColor,  

  7.                       (id)[UIColor yellowColor].CGColor,  

  8.                       (id)[UIColor greenColor].CGColor,  

  9.                       (id)[UIColor blueColor].CGColor,nil];  

  10.     animation.duration=3;  

  11.     animation.autoreverses=YES;  

  12.     //把關鍵幀添加到layer中  

  13.     [self.view.layer addAnimation:animation forKey:@"backgroundColor"];  

  14. }  



三、使用路徑製做動畫:CAKeyframeAnimation

[plain] view plaincopy

  1. -(void)animationOfCAKeyframeAnimationPath  

  2. {  

  3.     //初始化一個View,用來顯示動畫  

  4.     UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];  

  5.     redView.backgroundColor=[UIColor redColor];  

  6.       

  7.     [self.view addSubview:redView];  

  8.       

  9.     CAKeyframeAnimation *ani=[CAKeyframeAnimation animation];  

  10.     //初始化路徑  

  11.     CGMutablePathRef aPath=CGPathCreateMutable();  

  12.     //動畫起始點  

  13.     CGPathMoveToPoint(aPath, nil, 20, 20);  

  14.     CGPathAddCurveToPoint(aPath, nil,   

  15.                           160, 30,//控制點  

  16.                           220, 220,//控制點   

  17.                           240, 380);//控制點  

  18.       

  19.     ani.path=aPath;  

  20.     ani.duration=10;  

  21.     //設置爲漸出  

  22.     ani.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];  

  23.     //自動旋轉方向  

  24.     ani.rotationMode=@"auto";  

  25.       

  26.     [redView.layer addAnimation:ani forKey:@"position"];  

  27. }  

相關文章
相關標籤/搜索