标签:探花花花 动画 cabasicanimation ios ios开发
最近iOS开发中用到CoreAnimation的framework来做动画效果,虽然以前也用过,但一直没有系统学习过,今天看到一篇非常详细的博文(虽然是日语,但真的写的很好),在此翻译出来供大家学习。
CABasicAnimation类的使用方式就是基本的关键帧动画。
所谓关键帧动画,就是将Layer的属性作为KeyPath来注册,指定动画的起始帧和结束帧,然后自动计算和实现中间的过渡动画的一种动画方式。
CABasicAnimation的基本使用顺序
1.引用QuartzCore.framework
将"QuartzCore.framework"这个库添加到项目中。并且在需要使用CABaseAnimation类的地方import头文件。
-
#import <QuartzCore/QuartzCore.h>
2.CABaseAnimation的实例化以及关键路径的注册
使用"animationWithKeyPath:"方法进行CABasicAnimation的实例化,并指定Layer的属性作为关键路径来注册。
-
-
CABasicAnimation *animation =
-
[CABasicAnimation animationWithKeyPath:@"position"];
3.设定动画
设定动画的属性。以下是属性及其对应的说明:
属性 |
说明 |
duration |
动画时长(秒为单位)(注:此处与原文有出入) |
repeatCount |
重复次数。永久重复的话设置为HUGE_VALF。 |
beginTime |
指定动画开始时间。从开始指定延迟几秒执行的话,请设置为
「CACurrentMediaTime() + 秒数」的形式。 |
timingFunction |
设定动画的速度变化 |
autoreverses |
动画结束时是否执行逆动画 |
例:
-
animation.duration = 2.5;
-
animation.repeatCount = 1;
-
animation.beginTime = CACurrentMediaTime() + 2;
-
animation.autoreverses = YES;
-
-
-
animation.timingFunction =
-
[CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
4.设定动画的开始帧和结束帧
设定动画开始和结束帧时的状态。设定的值会变为KeyPath所指定的属性的值。
属性 |
说明 |
fromValue |
开始值 |
toValue |
终了值(絶対値) |
byValue |
终了值(相对值) |
例:
-
-
CABasicAnimation *animation =
-
[CABasicAnimation animationWithKeyPath:@"position"];
-
-
???
-
-
-
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
-
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(320, 480)];
5.添加动画
为Layer添加设置完成的动画,可以给Key指定任意名字。
-
[myView.layer addAnimation:animation forKey:@"move-layer"];
其他.动画结束后回到初始状态的现象的解决方法
用CABasicAnimation执行动画,在动画结束后会回归动画开始前的状态。想要解决的话,必须设置“removedOnCompletion”和“fillMode”这两个属性。
-
-
animation.removedOnCompletion = NO;
-
animation.fillMode = kCAFillModeForwards;
CABasicAnimation的使用示例
实际上CABasicAnimation有很多种使用方法,以下将一一列举。
移动动画
移动动画的代码如下:
-
-
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
-
-
-
animation.duration = 2.5;
-
animation.repeatCount = 1;
-
-
-
animation.fromValue = [NSValue valueWithCGPoint:myView.layer.position];
-
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(320, 480)];
-
-
-
[myView.layer addAnimation:animation forKey:@"move-layer"];
旋转动画
旋转动画的代码如下:
-
-
-
-
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
-
-
-
animation.duration = 2.5;
-
animation.repeatCount = 1;
-
-
-
animation.fromValue = [NSNumber numberWithFloat:0.0];
-
animation.toValue = [NSNumber numberWithFloat:22 * M_PI];
-
-
-
[myView.layer addAnimation:animation forKey:@"rotate-layer"];
缩放动画
缩放动画的代码如下:
-
-
-
-
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
-
-
-
animation.duration = 2.5;
-
animation.repeatCount = 1;
-
animation.autoreverses = YES;
-
-
-
animation.fromValue = [NSNumber numberWithFloat:1.0];
-
animation.toValue = [NSNumber numberWithFloat:2.0];
-
-
-
[myView.layer addAnimation:animation forKey:@"scale-layer"];
组合动画
使用CAAnimationGroup类进行复数动画的组合。代码如下:
-
-
CABasicAnimation *animation1 =
-
[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
-
-
-
animation1.toValue = [NSNumber numberWithFloat:80];;
-
-
-
-
CABasicAnimation *animation2 =
-
[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
-
-
-
animation2.fromValue = [NSNumber numberWithFloat:0.0];
-
animation2.toValue = [NSNumber numberWithFloat:44 * M_PI];
-
-
-
-
CAAnimationGroup *group = [CAAnimationGroup animation];
-
-
-
group.duration = 3.0;
-
group.repeatCount = 1;
-
-
-
group.animations = [NSArray arrayWithObjects:animation1, animation2, nil nil];
-
[myView.layer addAnimation:group forKey:@"move-rotate-layer"];
捕获动画开始时和终了时的事件
博主:设定委托对象,实现委托方法,如下:
-
-
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
-
animation.delegate = self;
-
-
-
animation.duration = 2.5;
-
animation.repeatCount = 1;
-
-
-
animation.toValue = [NSNumber numberWithFloat:100];;
-
-
-
[myView.layer addAnimation:animation forKey:@"move-layer"];
-
-
???
-
-
-
-
-
- (void)animationDidStart:(CAAnimation *)theAnimation
-
{
-
NSLog(@"begin");
-
}
-
-
-
-
-
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
-
{
-
NSLog(@"end");
-
}
CABasicAnimation使用时候的注意点
CABasicAnimation正在进行动画的时候,点击了Home按钮后再回到app的时候,动画会被清空。
Objective-C的示例程序
使用CABasicAnimation实现关键帧动画的示例程序如下:
-
- (void)viewDidLoad
-
{
-
[super viewDidLoad];
-
-
-
UIImage *image = [UIImage imageNamed:@"image01.png"];
-
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
-
imageView.center = CGPointMake(160, 240);
-
[self.view addSubview:imageView];
-
-
-
-
CABasicAnimation *animation1 =
-
[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
-
-
-
animation1.toValue = [NSNumber numberWithFloat:100];;
-
-
-
-
CABasicAnimation *animation2 =
-
[CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
-
-
-
animation2.toValue = [NSNumber numberWithFloat:66 * M_PI];
-
-
-
-
CAAnimationGroup *group = [CAAnimationGroup animation];
-
group.delegate = self;
-
group.duration = 5.0;
-
group.repeatCount = 1;
-
-
-
group.removedOnCompletion = NO;
-
group.fillMode = kCAFillModeForwards;
-
-
-
group.animations = [NSArray arrayWithObjects:animation1, animation2, nil nil];
-
[imageView.layer addAnimation:group forKey:@"move-rotate-layer"];
-
}
-
-
-
-
-
-
- (void)animationDidStart:(CAAnimation *)theAnimation
-
{
-
NSLog(@"begin");
-
}
-
-
-
-
-
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
-
{
-
NSLog(@"end");
-
}
示例程序的执行结果
Objective-C的示例程序的执行结果如下:
控制台输出如下:
CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
标签:探花花花 动画 cabasicanimation ios ios开发
原文地址:http://blog.csdn.net/q644419002/article/details/46914753