实现动画分三部曲:
(1)创建动画对象;
(2)设置动画属性(时长,重复次数...);
(3)将动画添加到图层(layer)上.
这几个Demo都是练习用的,比较简单
一.CABasicAnimation:基础动画
//获取相应的动画
CABasicAnimation
*animation = (CABasicAnimation
*)[_imgView.layer
animationForKey:@"rotationAnimation”];(@“rotationAnimation"表示动画类型)
1.缩放动画:
//(1)创建动画对象
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
//(2)设置动画属性
scaleAnimation.duration = 1;
scaleAnimation.fromValue = @1;
scaleAnimation.toValue = @2;
//设置动画结束后以动画形式恢复到原状态
scaleAnimation.autoreverses = YES;
//(3)将动画添加到图层
[_imgView.layer addAnimation:scaleAnimation forKey:nil];
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
//(2)设置动画属性
scaleAnimation.duration = 1;
scaleAnimation.fromValue = @1;
scaleAnimation.toValue = @2;
//设置动画结束后以动画形式恢复到原状态
scaleAnimation.autoreverses = YES;
//(3)将动画添加到图层
[_imgView.layer addAnimation:scaleAnimation forKey:nil];
2.左右抖动
//1.创建动画对象
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
//2.设置动画属性
//动画时间
rotationAnimation.duration = 0.1;
//相应属性的初始值
rotationAnimation.fromValue = @(-M_PI_4 *0.2);
//相应属性的结束值
rotationAnimation.toValue = @(M_PI_4 *0.2);
//重复次数
rotationAnimation.repeatCount = MAXFLOAT;
//动画结束后以动画形式回到远状态
rotationAnimation.autoreverses = YES;
//3.添加到图层
[_imgView.layer addAnimation:rotationAnimation forKey:nil];
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
//2.设置动画属性
//动画时间
rotationAnimation.duration = 0.1;
//相应属性的初始值
rotationAnimation.fromValue = @(-M_PI_4 *0.2);
//相应属性的结束值
rotationAnimation.toValue = @(M_PI_4 *0.2);
//重复次数
rotationAnimation.repeatCount = MAXFLOAT;
//动画结束后以动画形式回到远状态
rotationAnimation.autoreverses = YES;
//3.添加到图层
[_imgView.layer addAnimation:rotationAnimation forKey:nil];
二.CAKeyFrameAnimation:关键帧动画
基础动画可以实现的,关键帧动画都可以实现,反之则不然.
1.随机点运动:
//创建帧动画对象
CAKeyframeAnimation
*keyAnimation = [CAKeyframeAnimation
animationWithKeyPath:@"position"];
keyAnimation.duration =
2;//时长
//用可变数组存放随机点
NSMutableArray
*array = [[NSMutableArray
alloc]init];
//随机生成20个点
for (int i = 0; i < 20; i ++) {
CGPoint p = [self creatRandom];;
NSValue *value = [NSValue valueWithCGPoint:p];
[array addObject:value];
}
//将数组交给动画
//随机生成20个点
for (int i = 0; i < 20; i ++) {
CGPoint p = [self creatRandom];;
NSValue *value = [NSValue valueWithCGPoint:p];
[array addObject:value];
}
//将数组交给动画
keyAnimation.values = array;
//添加到layer层
[_imgView.layer
addAnimation:keyAnimation
forKey:nil];
2.圆周运动:
//以手指点击的地方作为圆心,150作为半径的圆运动
CAKeyframeAnimation
*keyAnimation = [CAKeyframeAnimation
animationWithKeyPath:@"position"];
keyAnimation.duration =
1;
keyAnimation.repeatCount
=
MAXFLOAT;
//创建运动路径
CGMutablePathRef
path =
CGPathCreateMutable();
//创建圆
CGPathAddArc(path,
NULL, touchPoint.x,
touchPoint.y,
100,
0,
M_PI
*2,
1);
keyAnimation.path
= path;
//释放路径
CGPathRelease(path);
[_imgView.layer
addAnimation:keyAnimation
forKey:nil];
三.CAAnimationGroup:组动画
//创建组动画对象
CAAnimationGroup *group = [CAAnimationGroup
animation];
//设置属性
group.duration =
5;(动画时长)
group.repeatCount =
MAXFLOAT; (重复次数:无数次)
group.delegate
=
self;(设置代理,用来监听动画的开始和结束,代理名:
CAMediaTiming)
//创建两个动画对象,添加到组动画中
CAAnimation
*animation1 = [self
movieWithArc:touvhPoint];
CAAnimation
*animation2 = [self
movieWithLeftAndRight
];
//将动画添加到动画组中
group.animations =
@[animation1,animation2];
//将动画添加到图层
[_imgView.layer
addAnimation:group
forKey:nil];
四.CATransition:转场动画
//创建转场动画对象
CATransition *transition = [[CATransition
alloc]init];
CACurrentMediaTime() (当前时间)
beginTime
(动画开始时间)
_imgView.layer.timeOffset
= 0; (时间偏移量,记录某一刻时间动画的进度)
_imgView.layer.speed
= 1; (动画速度,speed为1表示开始,为0表示停止动画)
1.动画类型:
苹果官方提供了几种动画类型:
kCATransitionFade
渐变
kCATransitionMoveIn
进入
kCATransitionPush
推入
kCATransitionReveal
移除
使用: transition.type
=
kCATransitionFade;
私有的动画效果:如果在app中使用的话,上架遭拒的可能较大,不过最近可能放宽政策了.
rippleEffect
水滴效果
cameraIrisHollowClose
相机关闭的效果
cameraIrisHollowOpen
相机打开的效果
cube
立体翻滚
oglFlip
上下左右翻滚效果
suckEffect
收缩效果,如一块布被抽走
pageCurl
向上翻页
pageUnCurl
向下翻页
使用:
与官方提供的使用方法有所不同
transition.type
=
@"pageUnCurl";
2.修改导航控制器的动画:
//动画类型
transition.type
=
@"suckEffect";
//动画子类型,设置动画从哪个方向开始,对有些动画效果不起作用
transition.subtype =
kCATransitionFromBottom;
//动画时长
transition.duration
=
1;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
SecondViewController *secondCtrl = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secondCtrl animated:NO];