在IOS中若是使用普通的動畫則可使用UIKit提供的動畫方式來實現,若是想實現更復雜的效果,則須要使用Core Animation了。數組
下面詳解各類類型動畫的使用方式app
[plain] view plaincopy動畫
-(void)animationOfUIKit spa
{ .net
UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)]; orm
redView.backgroundColor=[UIColor redColor]; 對象
[self.view addSubview:redView]; blog
//開始動畫 ip
[UIView beginAnimations:@"test" context:nil]; get
//動畫時長
[UIView setAnimationDuration:1];
/*
*要進行動畫設置的地方
*/
redView.backgroundColor=[UIColor blueColor];
redView.frame=CGRectMake(50, 50, 200, 200);
redView.alpha=0.5;
//動畫結束
[UIView commitAnimations];
}
[plain] view plaincopy
-(void)animationOfBlock
{
//初始化一個View,用來顯示動畫
UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
redView.backgroundColor=[UIColor redColor];
[self.view addSubview:redView];
[UIView animateWithDuration:1 //時長
delay:0 //延遲時間
options:UIViewAnimationOptionTransitionFlipFromLeft//動畫效果
animations:^{
//動畫設置區域
redView.backgroundColor=[UIColor blueColor];
redView.frame=CGRectMake(50, 50, 200, 200);
redView.alpha=0.5;
} completion:^(BOOL finish){
//動畫結束時調用
//............
}];
}
在Core Animation中咱們常常使用的是
CABasicAnimation
CAKeyframeAnimation
CATransitionAnimation
其中CABasicAnimation和CAKeyframeAnimation是對圖層中的不一樣屬性進行動畫的。
若是要多整個圖層進行動畫,則應該使用CATransitionAnimation
若是要使用組合動畫,例如要改變圖層的大小和透明度,則能夠先爲每一個屬性建立一個CABasicAnimation對象,再把他們組合到CAAnimationGroup中,最後把這個組合添加到要進行動畫的CALayer中。
注:CAAnimation(以及CAAnimation的子類),所有都是顯式動畫,這樣動畫播放後,表現層回恢復到模型層的原始狀態,這就意味着,若是動畫播放完後,會恢復到原來的樣子,因此在動畫播放完後要對模型層進行修改,例如:self.view.layer.backgroundColor=[UIColor blueColor].CGColor;
[plain] view plaincopy
-(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"];
}
1. path
這是一個 CGPathRef 對象,默認是空的,當咱們建立好CAKeyframeAnimation的實例的時候,能夠經過制定一個本身定義的path來讓 某一個物體按照這個路徑進行動畫。這個值默認是nil 當其被設定的時候 values 這個屬性就被覆蓋
2. values
一個數組,提供了一組關鍵幀的值, 當使用path的 時候 values的值自動被忽略。
下面是改變依次改變view的顏色
[plain] view plaincopy
-(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"];
}
[plain] view plaincopy
-(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"];
}