一、UIView基礎動畫
UIKit 直接將動畫繼承到UIView類中,當內部的一些屬性發生改變時,UIView將為這些改變提供動畫支持。執行動畫的工作由UIView類自動完成,但仍希望在執行動畫時通知視圖,為此需要將改變屬性的代碼放在[UIVIew beginAnimations:nil context:nil]與[UIView commitAnimations]之間。
1、UIView動畫
//UIView動畫(UIKit動畫)
-(void)viewAnimation{
//早期的動畫塊 在begin和commit之間寫需要改變的視圖
[UIView beginAnimations:@"test1" context:nil];
//設置動畫時長
[UIView setAnimationDuration:1.0];
//設置動畫是否反轉 動畫執行完畢,自動原路返回原狀態
[UIView setAnimationRepeatAutoreverses:YES];
//設置動畫重復次數
[UIView setAnimationRepeatCount:5];
//在兩者之間實現具體代碼
self.testView.backgroundColor=[UIColor yellowColor];
//改變大小
CGRect rect=self.testView.bounds;
rect.size=CGSizeMake(120, 120);
self.testView.bounds=rect;
//改變位置
CGRect frame=self.testView.frame;
frame.origin=CGPointMake(CGRectGetWidth(self.view.frame)-120, CGRectGetHeight(self.view.frame)-120);
self.testView.frame=frame;
//提交動畫
[UIView commitAnimations];
}
2、UIView層block動畫
//UIView層block動畫
-(void)blockViewAnimation{
//第一個參數:動畫的時長
// 第二個參數:將要添加動畫的代碼在block中實現,相當于將動畫的begin和commit之間的代碼寫在block中
// 先將動畫的設置配置完整,在改變屬性值
[UIView animateWithDuration:1.0 animations:^{
//設置動畫是否反轉 動畫執行完畢,自動原路返回原狀態
[UIView setAnimationRepeatAutoreverses:YES];
//設置動畫重復次數
[UIView setAnimationRepeatCount:5];
//要改變的屬性
self.testView.backgroundColor=[UIColor redColor];
//改變大小
CGRect rect=self.testView.bounds;
rect.size=CGSizeMake(120, 120);
self.testView.bounds=rect;
//改變位置
CGRect frame=self.testView.frame;
frame.origin=CGPointMake(CGRectGetWidth(self.view.frame)-120, CGRectGetHeight(self.view.frame)-120);
self.testView.frame=frame;
}];
//completion:動畫執行結束會執行的block
[UIView animateWithDuration:1.0 animations:^{
[UIView setAnimationRepeatAutoreverses:YES];
//設置動畫重復次數
[UIView setAnimationRepeatCount:5];
//要改變的屬性
self.testView.backgroundColor=[UIColor redColor];
} completion:^(BOOL finished) {
//當動畫執行完成,我們不需要該界面,可以在此處將動畫作用的界面隱藏或移除掉
[self.testView removeFromSuperview];
NSLog(@"動畫執行完畢");
}];
//delay:延時執行 多少秒之后在執行該動畫
//options:動畫一些效果的設置,例如是否重復等
[UIView animateWithDuration:1.0 delay:3.0 options:(UIViewAnimationOptionRepeat) animations:^{
self.testView.backgroundColor=[UIColor orangeColor];
} completion:^(BOOL finished) {
NSLog(@"執行完畢");
}];
//過渡動畫 當視圖進行切換或者視圖位置發生改變的時候,可以考慮使用該動畫
// [UIView transitionWithView:self.testView duration:1.5 options:(UIViewAnimationOptionTransitionFlipFromLeft) animations:^{
//
// self.testView.backgroundColor=[UIColor orangeColor];
// } completion:^(BOOL finished) {
// NSLog(@"執行完畢");
// }];
}
3、關鍵幀動畫
//關鍵幀動畫
-(void)keyFrameAnimation{
[UIView animateKeyframesWithDuration:4.0 delay:0.0 options:(UIViewKeyframeAnimationOptionRepeat) animations:^{
//添加動畫所需要的幀數
//第一個參數:當前幀相對的開始時間,取值范圍為0~1 例如:如果相對開始時間為0.5,那么他的總時長為0.5*4=2s
//第二個參數:當前幀相對的時長,取值范圍為0~1,例如:此處給值0.5,那么此幀的播放時長為0.5*4=2s
//第三個參數為:block,在block中實現動畫所要做的操作
//第一幀
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.25 animations:^{
CGRect rect=self.testView.frame;
rect.origin.x=CGRectGetWidth(self.view.frame)-rect.size.width;
self.testView.frame=rect;
}];
//第二幀
[UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.25 animations:^{
CGRect rect=self.testView.frame;
rect.origin.y=CGRectGetHeight(self.view.frame)-rect.size.height;
self.testView.frame=rect;
}];
//第三幀
[UIView addKeyframeWithRelativeStartTime:0.50 relativeDuration:0.25 animations:^{
CGRect rect=self.testView.frame;
rect.origin.x=0;
self.testView.frame=rect;
}];
//第四幀
[UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
CGRect rect=self.testView.frame;
rect.origin.y=0;
self.testView.frame=rect;
}];
} completion:^(BOOL finished) {
NSLog(@"執行完畢");
}];
}
4、spring動畫
//spring動畫
-(void)springAnimation{
//usingSpringWithDamping:阻尼,取值范圍為0-1,值越小,動畫效果越明顯,抖動的次數多
//initialSpringVelocity:初始速率,值越大,動畫效果越明顯,彈得距離大
//options:動畫效果
[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.1 initialSpringVelocity:20.0 options:(UIViewAnimationOptionCurveLinear) animations:^{
[UIView setAnimationRepeatAutoreverses:YES];
self.testView.center=self.view.center;
} completion:^(BOOL finished) {
self.testView.center=CGPointMake(50, 50);
}];
}
二、CoreAnimation動畫
1、CoreAnimation動畫位于iOS框架的Media層需要添加QuartzCore.Framework(系統自動添加)CoreAnimation基本上是Layer Animation
- CALayer負責繪制,提供UIView需要展示的內容,不能交互。
- UIView負責交互,顯示CALayer繪制的內容。Layer有很多種,最常用和最基本的是CALayer,還有它的子類:
- CAScrollerLayer簡化顯示層的一部分
- CATextLayer文本層
- CAGradientLayer 可以方便的處理顏色漸變、CAShapeLayer僅僅限于沿著邊緣的動畫效果,它實現不了填充效果
//layer層的動畫
//basicAnimation
-(void)basicAnimation{
//keyPath:想要改變layer的屬性或者是layer的屬性的屬性
//keyPath如果書寫錯誤,動畫沒有效果
CABasicAnimation* basicAni=[CABasicAnimation animationWithKeyPath:@"position.x"];
//如果我們只設置toValue一個屬性,那就是默認從當前位置到toValue的位置
// basicAni.toValue=@(CGRectGetWidth(self.view.frame)-50);
//如果我們只設置byValue屬性,那么就是默認從當前位置加byValue的值
basicAni.byValue=@50;
//讓動畫結束之后,不返回原位置
//當動畫結束的時候,是否移除動畫所產生的效果
basicAni.removedOnCompletion=NO;
//保持哪種狀態
//kCAFillModeForwards:保持動畫結束的狀態
basicAni.fillMode=kCAFillModeForwards;
//fromValue:必須和toValue或者byValue兩者中的一個結合使用
basicAni.fromValue=@100;
//設置動畫時長
[basicAni setDuration:2.0];
//將動畫添加到layer層
//forKey:動畫標記,移除動畫時使用
[self.testView.layer addAnimation:basicAni forKey:@"basicAni"];
}
//layer層的keyframeAnimation
-(void)layerKeyframeAnimation{
//初始化
CAKeyframeAnimation* keyFrameAni=[CAKeyframeAnimation animationWithKeyPath:@"position"];
//得到視圖原始的position
CGPoint originPoint=self.testView.layer.position;
NSValue* originValue=[NSValue valueWithCGPoint:originPoint];
//一幀
CGPoint firstPoint=CGPointMake(50, 50);
NSValue* firstValue=[NSValue valueWithCGPoint:firstPoint];
//二幀
CGPoint secondPoint=CGPointMake(CGRectGetWidth(self.view.frame)-50, 50);
NSValue* secondValue=[NSValue valueWithCGPoint:secondPoint];
//將幀添加到layer上
keyFrameAni.values=@[originValue,firstValue,secondValue];
//將動畫添加到layer上
[self.testView.layer addAnimation:keyFrameAni forKey:@"keyFrameAni"];
keyFrameAni.duration=10.0;
keyFrameAni.removedOnCompletion=NO;
keyFrameAni.fillMode=kCAFillModeForwards;
}