CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
来源:互联网 发布:java plug in是什么 编辑:程序博客网 时间:2024/06/06 02:03
CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
CABasicAnimation类的使用方式就是基本的关键帧动画。
所谓关键帧动画,就是将Layer的属性作为KeyPath来注册,指定动画的起始帧和结束帧,然后自动计算和实现中间的过渡动画的一种动画方式。
CABasicAnimation的基本使用顺序
1.引用QuartzCore.framework
将"QuartzCore.framework"这个库添加到项目中。并且在需要使用CABaseAnimation类的地方import头文件。
#import <QuartzCore/QuartzCore.h>
2.CABaseAnimation的实例化以及关键路径的注册
使用"animationWithKeyPath:"方法进行CABasicAnimation的实例化,并指定Layer的属性作为关键路径来注册。
// 指定position属性CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
3.设定动画
设定动画的属性。以下是属性及其对应的说明:
「CACurrentMediaTime() + 秒数」的形式。timingFunction设定动画的速度变化autoreverses动画结束时是否执行逆动画例:
animation.duration = 2.5; // 动画持续时间animation.repeatCount = 1; // 不重复animation.beginTime = CACurrentMediaTime() + 2; // 2秒后执行animation.autoreverses = YES; // 结束后执行逆动画 // 动画先加速后减速animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
4.设定动画的开始帧和结束帧
设定动画开始和结束帧时的状态。设定的值会变为KeyPath所指定的属性的值。// 指定position属性(移动)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"];
旋转动画
旋转动画的代码如下:
/* 旋转 */ // 对Y轴进行旋转(指定Z轴的话,就和UIView的动画一样绕中心旋转)CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"]; // 设定动画选项animation.duration = 2.5; // 持续时间animation.repeatCount = 1; // 重复次数 // 设定选装角度animation.fromValue = [NSNumber numberWithFloat:0.0]; // 起始角度animation.toValue = [NSNumber numberWithFloat:2 * 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类进行复数动画的组合。代码如下:
/* 动画1(在X轴方向移动) */CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; // 终点设定animation1.toValue = [NSNumber numberWithFloat:80];; // 終点 /* 动画2(绕Z轴中心旋转) */CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; // 设定旋转角度animation2.fromValue = [NSNumber numberWithFloat:0.0]; // 开始时的角度animation2.toValue = [NSNumber numberWithFloat:4 * M_PI]; // 结束时的角度 /* 动画组 */CAAnimationGroup *group = [CAAnimationGroup animation]; // 动画选项设定group.duration = 3.0;group.repeatCount = 1; // 添加动画group.animations = [NSArray arrayWithObjects:animation1, animation2, 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"]; // 绕x轴转3圈 animation2.toValue = [NSNumber numberWithFloat:6 * 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]; [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的示例程序的执行结果如下:
控制台输出如下:
beginend
0 0
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- C++ 代码分块也可以的
- generate crash report while building your application
- C/C++中static关键字作用总结
- opener.document.all是什么意思(转贴)
- CodeForces 396C
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- 20140820
- C语言之预处理
- 这些美食微信上卖得火看看他们如何聚集人气
- poj 3358 Period of an Infinite Binary Expansion(数论:欧拉函数+快速幂取模)
- jsessionid所引起的问题 和解决
- uIP介绍
- jdk安装和环境变量所引发的思考
- 打字