iOS開發之Core Animation

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

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

  • CABasicAnimation
  • CAKeyframeAnimation
  • CATransitionAnimation

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

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

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

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

一、自定義動畫:CABasicAnimation

-(void)animationOfCABasicAnimation
{
    //建立一個CABasicAnimation對象
    CABasicAnimation *animation=[CABasicAnimation animation];
    //設置顏色
    animation.toValue=(id)[UIColor blueColor].CGColor;
    //動畫時間
    animation.duration=1;
    //是否反轉變爲原來的屬性值
    animation.autoreverses=YES;
    //把animation添加到圖層的layer中,即可以播放動畫了。forKey指定要應用此動畫的屬性
    [self.view.layer addAnimation:animation forKey:@"backgroundColor"];
    
}

二、關鍵幀動畫:CAKeyframeAnimation

 

1. path對象

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

2. valuesip

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

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

-(void)animationOfCAKeyframeAnimation
{
    CAKeyframeAnimation *animation=[CAKeyframeAnimation animation];
    //設置屬性值
    animation.values=[NSArray arrayWithObjects:
                      (id)self.view.backgroundColor,
                      (id)[UIColor yellowColor].CGColor,
                      (id)[UIColor greenColor].CGColor,
                      (id)[UIColor blueColor].CGColor,nil];
    animation.duration=3;
    animation.autoreverses=YES;
    //把關鍵幀添加到layer中
    [self.view.layer addAnimation:animation forKey:@"backgroundColor"];
}

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

-(void)animationOfCAKeyframeAnimationPath
{
    //初始化一個View,用來顯示動畫
    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
    redView.backgroundColor=[UIColor redColor];
    
    [self.view addSubview:redView];
    
    CAKeyframeAnimation *ani=[CAKeyframeAnimation animation];
    //初始化路徑
    CGMutablePathRef aPath=CGPathCreateMutable();
    //動畫起始點
    CGPathMoveToPoint(aPath, nil, 20, 20);
    CGPathAddCurveToPoint(aPath, nil, 
                          160, 30,//控制點
                          220, 220,//控制點 
                          240, 380);//控制點
    
    ani.path=aPath;
    ani.duration=10;
    //設置爲漸出
    ani.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    //自動旋轉方向
    ani.rotationMode=@"auto";
    
    [redView.layer addAnimation:ani forKey:@"position"];
}

源碼下載:點擊下載源碼

原文:http://blog.csdn.net/kqjob/article/details/10417461

---文章完---

最後,推薦一個神器。

內測寶

內測寶我的以爲比TestFlight更簡單好用,開發者只須要簡單把打好的ipa包上傳上去,生成二維碼,測試人員在手機上掃碼二維碼,就能夠直接安裝最新的測試版本了,好用的讓人想哭。

相關文章
相關標籤/搜索