最近寫項目發(fā)現(xiàn)越來越?jīng)]動力了汁讼,
底層的東西實在太讓人頭疼了淆攻。。嘿架。
所幸研究一點好玩的東西~~那就是特效F可骸!耸彪!
biubiubibuiubiu~~~~~~
好了廢話不說 開始講解 各種常見的不常見的甚至沒見過的 iOS自帶特效伞芹!
1. 基礎(chǔ)動畫篇
基礎(chǔ)動畫-位移
基礎(chǔ)動畫-位移gif.gif
/**
* 位移動畫演示
*/
-(void)positionAnimation{
//使用CABasicAnimation創(chuàng)建基礎(chǔ)動畫
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"position"];
anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-75)];
anima.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-75)];
anima.duration = 1.0f;
// 如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那么在動畫執(zhí)行完畢后搜囱,圖層會保持顯示動畫執(zhí)行后的狀態(tài)丑瞧。但在實質(zhì)上,圖層的屬性值還是動畫執(zhí)行前的初始值蜀肘,并沒有真正被改變绊汹。
//anima.fillMode = kCAFillModeForwards;
//anima.removedOnCompletion = NO;
anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[_demoView.layer addAnimation:anima forKey:@"positionAnimation"];
//使用UIView Animation 代碼塊調(diào)用
// _demoView.frame = CGRectMake(0, SCREEN_HEIGHT/2-50, 50, 50);
// [UIView animateWithDuration:1.0f animations:^{
// _demoView.frame = CGRectMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-50, 50, 50);
// } completion:^(BOOL finished) {
// _demoView.frame = CGRectMake(SCREEN_WIDTH/2-25, SCREEN_HEIGHT/2-50, 50, 50);
// }];
//
//使用UIView [begin,commit]模式
// _demoView.frame = CGRectMake(0, SCREEN_HEIGHT/2-50, 50, 50);
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:1.0f];
// _demoView.frame = CGRectMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-50, 50, 50);
// [UIView commitAnimations];
}
基礎(chǔ)動畫-透明度
基礎(chǔ)動畫-透明度gif.gif
/**
* 透明度動畫
*/
-(void)opacityAniamtion{
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"opacity"];
anima.fromValue = [NSNumber numberWithFloat:1.0f];
anima.toValue = [NSNumber numberWithFloat:0.2f];
anima.duration = 1.0f;
[_demoView.layer addAnimation:anima forKey:@"opacityAniamtion"];
}
基礎(chǔ)動畫-縮放
基礎(chǔ)動畫-縮放gif.gif
/**
* 縮放動畫
*/
-(void)scaleAnimation{
// CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"bounds"];
// anima.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
// anima.duration = 1.0f;
// [_demoView.layer addAnimation:anima forKey:@"scaleAnimation"];
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//同上
anima.toValue = [NSNumber numberWithFloat:2.0f];
anima.duration = 1.0f;
[_demoView.layer addAnimation:anima forKey:@"scaleAnimation"];
// CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
// anima.toValue = [NSNumber numberWithFloat:0.2f];
// anima.duration = 1.0f;
// [_demoView.layer addAnimation:anima forKey:@"scaleAnimation"];
}
基礎(chǔ)動畫-旋轉(zhuǎn)
基礎(chǔ)動畫-旋轉(zhuǎn)gif.gif
/**
* 旋轉(zhuǎn)動畫
*/
-(void)rotateAnimation{
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//繞著z軸為矢量,進行旋轉(zhuǎn)(@"transform.rotation.z"==@@"transform.rotation")
anima.toValue = [NSNumber numberWithFloat:M_PI];
anima.duration = 1.0f;
[_demoView.layer addAnimation:anima forKey:@"rotateAnimation"];
// //valueWithCATransform3D作用與layer
// CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform"];
// anima.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0, 0, 1)];//繞著矢量(x,y,z)旋轉(zhuǎn)
// anima.duration = 1.0f;
// //anima.repeatCount = MAXFLOAT;
// [_demoView.layer addAnimation:anima forKey:@"rotateAnimation"];
// //CGAffineTransform作用與View
// _demoView.transform = CGAffineTransformMakeRotation(0);
// [UIView animateWithDuration:1.0f animations:^{
// _demoView.transform = CGAffineTransformMakeRotation(M_PI);
// } completion:^(BOOL finished) {
//
// }];
}
基礎(chǔ)動畫-變色
基礎(chǔ)動畫-背景色gif.gif
./**
* 背景色變化動畫
*/
-(void)backgroundAnimation{
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
anima.toValue =(id) [UIColor greenColor].CGColor;
anima.duration = 1.0f;
[_demoView.layer addAnimation:anima forKey:@"backgroundAnimation"];
}
2 幀動畫
幀動畫-關(guān)鍵幀
幀動畫-關(guān)鍵幀.gif
/**
* 關(guān)鍵幀動畫
*/
-(void)keyFrameAnimation{
CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
NSValue *value0 = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-50)];
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2-50)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2+50)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2+50)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2-50)];
NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-50)];
anima.values = [NSArray arrayWithObjects:value0,value1,value2,value3,value4,value5, nil];
anima.duration = 2.0f;
anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];//設(shè)置動畫的節(jié)奏
anima.delegate = self;//設(shè)置代理扮宠,可以檢測動畫的開始和結(jié)束
[_demoView.layer addAnimation:anima forKey:@"keyFrameAnimation"];
}
-(void)animationDidStart:(CAAnimation *)anim{
NSLog(@"開始動畫");
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
NSLog(@"結(jié)束動畫");
}
幀動畫-路徑
幀動畫-路徑.gif
/**
* path動畫
*/
-(void)pathAnimation{
CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(SCREEN_WIDTH/2-100, SCREEN_HEIGHT/2-100, 200, 200)];
anima.path = path.CGPath;
anima.duration = 2.0f;
[_demoView.layer addAnimation:anima forKey:@"pathAnimation"];
}
幀動畫-抖動
幀動畫-抖動.gif
/**
* 抖動效果
*/
-(void)shakeAnimation{
CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];//在這里@"transform.rotation"==@"transform.rotation.z"
NSValue *value1 = [NSNumber numberWithFloat:-M_PI/180*4];
NSValue *value2 = [NSNumber numberWithFloat:M_PI/180*4];
NSValue *value3 = [NSNumber numberWithFloat:-M_PI/180*4];
anima.values = @[value1,value2,value3];
anima.repeatCount = MAXFLOAT;
[_demoView.layer addAnimation:anima forKey:@"shakeAnimation"];
}
注意:抖動最好加一個timer時間判斷 不然她會抖到你手機沒電西乖!
幀動畫-彈來彈去
幀動畫-彈來彈去.gif
//彈來彈去
-(void)calculationAnimation
{
//需要實現(xiàn)的幀動畫
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"transform.scale";
animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
animation.duration = 1;
animation.calculationMode = kCAAnimationCubic;
//把動畫添加上去
[_demoView.layer addAnimation:animation forKey:nil];
}
這個動畫 是一個我個人很喜歡的動畫 可能這么看 你感覺不出 他有什么用狐榔? 這里附加一個實例的動畫 我想你們會收到啟發(fā)的
幀動畫-彈來彈去實例.gif
3 動畫簡單組合
組合動畫-同時進行
組合動畫-同時進行.gif
-(void)groupAnimation1{
// //位移動畫
CAKeyframeAnimation *anima1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
NSValue *value0 = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-50)];
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2-50)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2+50)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2+50)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2-50)];
NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-50)];
anima1.values = [NSArray arrayWithObjects:value0,value1,value2,value3,value4,value5, nil];
//縮放動畫
CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
anima2.fromValue = [NSNumber numberWithFloat:0.8f];
anima2.toValue = [NSNumber numberWithFloat:2.0f];
//旋轉(zhuǎn)動畫
CABasicAnimation *anima3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
anima3.toValue = [NSNumber numberWithFloat:M_PI*4];
//組動畫
CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
groupAnimation.animations = [NSArray arrayWithObjects:anima1,anima2,anima3, nil];
groupAnimation.duration = 4.0f;
[_demoView.layer addAnimation:groupAnimation forKey:@"groupAnimation"];
//-------------如下,使用三個animation不分裝成group获雕,只是把他們添加到layer薄腻,也有組動畫的效果。-------------
// //位移動畫
// CAKeyframeAnimation *anima1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
// NSValue *value0 = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-50)];
// NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2-50)];
// NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2+50)];
// NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2+50)];
// NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2-50)];
// NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-50)];
// anima1.values = [NSArray arrayWithObjects:value0,value1,value2,value3,value4,value5, nil];
// anima1.duration = 4.0f;
// [_demoView.layer addAnimation:anima1 forKey:@"aa"];
//
// //縮放動畫
// CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
// anima2.fromValue = [NSNumber numberWithFloat:0.8f];
// anima2.toValue = [NSNumber numberWithFloat:2.0f];
// anima2.duration = 4.0f;
// [_demoView.layer addAnimation:anima2 forKey:@"bb"];
//
// //旋轉(zhuǎn)動畫
// CABasicAnimation *anima3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
// anima3.toValue = [NSNumber numberWithFloat:M_PI*4];
// anima3.duration = 4.0f;
// [_demoView.layer addAnimation:anima3 forKey:@"cc"];
}
組合動畫-連續(xù)進行
組合動畫-連續(xù)進行.gif
/**
* 順序執(zhí)行的組動畫
*/
-(void)groupAnimation2{
CFTimeInterval currentTime = CACurrentMediaTime();
//位移動畫
CABasicAnimation *anima1 = [CABasicAnimation animationWithKeyPath:@"position"];
anima1.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-75)];
anima1.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2-75)];
anima1.beginTime = currentTime;
anima1.duration = 1.0f;
anima1.fillMode = kCAFillModeForwards;
anima1.removedOnCompletion = NO;
[_demoView.layer addAnimation:anima1 forKey:@"aa"];
//縮放動畫
CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
anima2.fromValue = [NSNumber numberWithFloat:0.8f];
anima2.toValue = [NSNumber numberWithFloat:2.0f];
anima2.beginTime = currentTime+1.0f;
anima2.duration = 1.0f;
anima2.fillMode = kCAFillModeForwards;
anima2.removedOnCompletion = NO;
[_demoView.layer addAnimation:anima2 forKey:@"bb"];
//旋轉(zhuǎn)動畫
CABasicAnimation *anima3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
anima3.toValue = [NSNumber numberWithFloat:M_PI*4];
anima3.beginTime = currentTime+2.0f;
anima3.duration = 1.0f;
anima3.fillMode = kCAFillModeForwards;
anima3.removedOnCompletion = NO;
[_demoView.layer addAnimation:anima3 forKey:@"cc"];
}
4. 結(jié)束語
以上就是一些簡單而且實用的 基礎(chǔ)動畫
雖然看上去gif 都挺丑的
但是我相信 如果你能把這些效果用好
不管你的app 實際內(nèi)容有多少 精不精彩 至少只是一個效果 界面
就足以讓你的app 發(fā)光發(fā)彩=彀浮b挚!
最后老規(guī)矩楣颠。
老貓在這里祝大家:永無BUG尽纽。不再加班!