CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
出处:http://blog.csdn.net/iosevanhuang/article/details/14488239
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.设定动画
设定动画的属性。以下是属性及其对应的说明:
属性 | 说明 |
---|---|
duration | 动画时长(秒为单位)(注:此处与原文有出入) |
repeatCount | 重复次数。永久重复的话设置为HUGE_VALF。 |
beginTime | 指定动画开始时间。从开始指定延迟几秒执行的话,请设置为 「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所指定的属性的值。
属性 | 说明 |
---|---|
fromValue | 开始值 |
toValue | 终了值(絶対値) |
byValue | 终了值(相对值) |
例:
// 指定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的示例程序的执行结果如下:
控制台输出如下:
begin
end
CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)的更多相关文章
- imageView图片放大缩小及旋转
imageView图片放大缩小及旋转 一.简介 二.方法 1)设置图片放大缩小效果 第一步:将<ImageView>标签中的android:scaleType设置为"fitCen ...
- Android DIY之路 (一) 指定区域多图片合成 放大 缩小 镜像 旋转 等(转)
惯例先看效果图 // 注意做类似这种模板功能时候 方位由后台数据提供,这里我们用假数据 4个点 或者xy 加区域来做示例 //一开始我们公司用的是透明盖住 操作图片 但发现 局限性较大.后来直接限定区 ...
- 猫猫学IOS(二)UI之button操作 点击变换 移动 放大缩小 旋转
不多说,先上图片看效果,猫猫分享.必须精品 原创文章.欢迎转载.转载请注明:翟乃玉的博客 地址:viewmode=contents">http://blog.csdn.net/u013 ...
- WPF多点触摸放大缩小旋转
原文:WPF多点触摸放大缩小旋转 版权声明:本文为博主原创文章,需要转载尽管转载. https://blog.csdn.net/z5976749/article/details/40118437 如果 ...
- AJ学IOS(02)UI之按钮操作 点击变换 移动 放大缩小 旋转
不多说,先上图片看效果,AJ分享,必须精品 这个小程序主要实现点击方向键可以让图标上下左右动还有放大缩小以及旋转的功能,点击图片会显示另一张图片. 点击变换 其实用到了按钮的两个状态,再State C ...
- JS控制图片拖动 放大 缩小 旋转 支持滚轮放大缩小 IE有效
<html> <head> <title>图片拖动,放大,缩小,转向</title> <script type="text/ja ...
- javascript仿新浪微博图片放大缩小及旋转效果
javascript仿新浪微博图片放大缩小及旋转效果 经常看到新浪微博里有图片放大缩小旋转效果,感觉效果还不错,所以就想试着做一个类似的demo出来,至于旋转对于IE可以用滤镜来解决,标准的浏览器可以 ...
- PhotoView实现图片随手势的放大缩小的效果
项目需求:在listView的条目中如果有图片,点击条目,实现图片的放大,并且图片可以根据手势来控制图片放大缩小的比例.类似于微信朋友圈中查看好友发布的照片所实现的效果. 思路是这样的:当点击条目的时 ...
- HTML5 canvas处理图片的各种效果,包括放大缩小涂鸦等
http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201502151385.html jQuery 缩放 旋转 裁剪图片 Image Cropper ...
随机推荐
- Hibernate映射文件创建和删除触发器,存储过程等数据库对象
创建表,触发器,和存储过程等数据库对象,这里一定要用create 在hibernate.cfg.xml文件中
- 使用scp在windows和Linux之间互传文件
转自:http://yangzhongfei.blog.163.com/blog/static/4610987520103141050918/ 为了进行系统维护操作,有时需要再windows和linu ...
- Selenium碰到的异常记录
.markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(171, 178 ...
- .net连接DB2的异常SQL0666 - SQL query exceeds specified time limit or storage limit.错误处理
SQL0666 - SQL query exceeds specified time limit or storage limit. 原因:查询超时 解决办法: set the DbCommand.C ...
- Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml
转自:http://www.cnblogs.com/wj-wangjun/archive/2009/10/21/1587624.html Hibernate SQL方言 (hibernate.dial ...
- JS实现页面打印
[原文链接] 1.js实现(可实现局部打印) <input id="btnPrint" type="button" value="打印" ...
- OCP笔记001
2. View the Exhibit to examine the description for the SALES table. Which views can have all DML ope ...
- raw转qcow2
raw转qcow2命令 : qemu-img convert -f raw test.img -O qcow2 test.img.qcow2 转晚之后,还需要virsh edit test,修改里面的 ...
- python3 生成器&迭代器
#Author by Andy#_*_ coding:utf-8 _*_import timefrom collections import Iterable#列表生成式def func(): lis ...
- Ubuntu中由root用户修改为普通用户的办法
比如你的普通用户名是test 目前是root用户 键入命令 su - test 就可以了