Core Animation2-CABasicAnimation

CABasicAnimation是CAPropertyAnimation的子类,使用它可以实现一些基本的动画效果,它可以让CALayer的某个属性从某个值渐变到另一个值。下面就用CABasicAnimation实现几个简单的动画。

* 先初始化一个UIView添加到控制器的view中,然后在这个UIView的layer上执行动画,下面的self是指控制器

复制代码
复制代码
1 _myView = [[UIView alloc] init];
2 _myView.layer.position = CGPointMake(100, 100);
3 _myView.layer.bounds = CGRectMake(0, 0, 100, 100);
4 _myView.backgroundColor = [UIColor blueColor];
5 [self.view addSubview:_myView];
6 [_myView release];
复制代码
复制代码

一、平移动画

实现平移动画有好几种方法,这里列举2种。

1.方法1

复制代码
复制代码
 1 // 说明这个动画对象要对CALayer的position属性执行动画
 2 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
 3 // 动画持续1.5s
 4 anim.duration = 1.5; 
 5 
 6 // position属性值从(50, 80)渐变到(300, 350)
 7 anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 80)];
 8 anim.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 350)];
 9 
10 // 设置动画的代理
11 anim.delegate = self;
12 
13 // 保持动画执行后的状态
14 anim.removedOnCompletion = NO;
15 anim.fillMode = kCAFillModeForwards;
16 
17 // 添加动画对象到图层上
18 [_myView.layer addAnimation:anim forKey:@"translate"];
复制代码
复制代码

* 第2行设置的keyPath是@"position",说明要修改的是CALayer的position属性,也就是会执行平移动画

* 注意第7、8行,这里并不是直接使用CGPoint这种结构体类型,而是要先包装成NSValue对象后再使用。这2行代码表示CALayer从位置(50, 80)移动到位置(300, 350)

* 如果将第8行的toValue换成byValue,代表CALayer从位置(50, 80)开始向右移动300、向下移动350,也就是移动到位置(350, 430)

* 默认情况下,动画执行完毕后,动画会自动从CALayer上移除,CALayer又会回到原来的状态。为了保持动画执行后的状态,可以加入第14、15行代码

* 第18行后面的@"translate"是给动画对象起个名称,以后可以调用CALayer的removeAnimationForKey:方法根据动画名称停止相应的动画

* 第11行是设置动画的代理,可以监听动画的执行过程,这里设置控制器为代理。代理需要实现的方法有:

复制代码
复制代码
 1 #pragma mark 动画开始
 2 - (void)animationDidStart:(CAAnimation *)anim {
 3     NSLog(@"动画开始了");
 4 }
 5 
 6 #pragma mark 动画结束
 7 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
 8     // 查看一下动画执行完毕后的position值
 9     NSString *string = NSStringFromCGPoint(_myView.layer.position);
10     NSLog(@"动画结束了,position:%@", string);
11 }
复制代码
复制代码

打印结果为:

1 2013-04-14 23:44:26.197 CAAnimation[5995:c07] 动画开始了
2 2013-04-14 23:44:27.697 CAAnimation[5995:c07] 动画结束了,position:{100, 100}

从第2行的打印信息可以看出,实际上,动画执行完毕后,并没有真正改变CALayer的position属性的值!

2.方法2

复制代码
复制代码
1 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
2 anim.duration = 1;
3 
4 CATransform3D form = CATransform3DMakeTranslation(350, 350, 0);
5 anim.toValue = [NSValue valueWithCATransform3D:form];
6 
7 [_myView.layer addAnimation:anim forKey:nil];
复制代码
复制代码

通过CALayer的transform属性实现平移动画,layer会从自己的初始位置平移到(350, 350)位置

二、缩放动画

实现缩放动画有好几种方法,这里列举2种。

1.方法1

复制代码
复制代码
1 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"];
2 anim.duration = 2;
3 
4 anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 30, 30)];
5 
6 [_myView.layer addAnimation:anim forKey:nil];
复制代码
复制代码

layer会从原来的尺寸(100x100)变为30x30

2.方法2

复制代码
复制代码
1 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
2 anim.duration = 1.5; // 动画持续1.5s
3 
4 // CALayer的宽度从0.5倍变为2倍
5 // CALayer的高度从0.5倍变为1.5倍
6 anim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1)];
7 anim.toValue  = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2, 1.5, 1)];
8 
9 [_myView.layer addAnimation:anim forKey:nil];
复制代码
复制代码

三、旋转动画

复制代码
复制代码
1 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
2 anim.duration = 1.5;
3 
4 // 绕着(0, 0, 1)这个向量轴顺时针旋转45°
5 anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];
6 
7 [_myView.layer addAnimation:anim forKey:nil]; 
复制代码
复制代码

其实可以不用设置fromValue,这里只设置了toValue

四、其他

* 除开前面使用的position、transform属性,其实CALayer还有好多属性都可以形成动画,这些属性统称为"Animatable Properties"。在《CALayer3-层的属性》开头有介绍如何搜索这些属性

* CABasicAnimation虽然能够做很多基本的动画效果,但是有个局限性,只能让CALayer的属性从某个值渐变到另一个值,仅仅是在2个值之间渐变

原文地址:https://www.cnblogs.com/geek6/p/3958766.html