简单的创建一个带路径的动画效果,比较粗糙,不过事先原理都是一样的,

代码如下:

1,创建动画所需的view

-(void)creatView
{
moveView = [UIView new]; moveView.backgroundColor = [UIColor purpleColor]; moveView.frame = CGRectMake(, , , ); [self.view addSubview:moveView];
}

2,动画的实现:

方法一:

-(void)startAnimation
{
// 方法一 用法1​ Value方式
//创建动画对象 CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; //设置value NSValue *value1=[NSValue valueWithCGPoint:CGPointMake(, )]; NSValue *value2=[NSValue valueWithCGPoint:CGPointMake(, )]; NSValue *value3=[NSValue valueWithCGPoint:CGPointMake(, )]; NSValue *value4=[NSValue valueWithCGPoint:CGPointMake(, )]; NSValue *value5=[NSValue valueWithCGPoint:CGPointMake(, )]; NSValue *value6=[NSValue valueWithCGPoint:CGPointMake(, )]; animation.values=@[value1,value2,value3,value4,value5,value6]; //重复次数 默认为1 animation.repeatCount=MAXFLOAT; //设置是否原路返回默认为不 animation.autoreverses = YES; //设置移动速度,越小越快 animation.duration = 4.0f; animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards; animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; animation.delegate=self; //给这个view加上动画效果 [moveView.layer addAnimation:animation forKey:nil]; }

方法二:

//    用法2​  Path方式

    //创建动画对象

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

    //创建一个CGPathRef对象,就是动画的路线

    CGMutablePathRef path = CGPathCreateMutable();

    //自动沿着弧度移动

    CGPathAddEllipseInRect(path, NULL, CGRectMake(, , , ));

    //设置开始位置

    CGPathMoveToPoint(path,NULL,,);

    //沿着直线移动

    CGPathAddLineToPoint(path,NULL, , );

    CGPathAddLineToPoint(path,NULL, , );

    CGPathAddLineToPoint(path,NULL, , );

    CGPathAddLineToPoint(path,NULL, , );

    CGPathAddLineToPoint(path,NULL, , );

    //沿着曲线移动

    CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,70.0,120.0);

    CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,90.0,120.0);

    CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,110.0,120.0);

    CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,130.0,120.0);

    animation.path=path;

    CGPathRelease(path);

    animation.autoreverses = YES;

    animation.repeatCount=MAXFLOAT;

    animation.removedOnCompletion = NO;

    animation.fillMode = kCAFillModeForwards;

    animation.duration = 4.0f;

    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    animation.delegate=self;

    [moveView.layer addAnimation:animation forKey:nil];
}

按照上面的方法,即可实现一个动画,参照上面的逻辑可以实现添加购物车,删除等带路径的动画!

下面附一个添加购物车的动画效果:

1,创建动画view

-(void)relodata
{
self.addCartImg = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/, self.view.frame.size.height+, , )];
self.addCartImg.hidden = true;
[self.view addSubview:self.addCartImg];
self.addCartImg.image = [UIImage imageNamed:@"3.jpg"];
}

2,具体动画的实现:

- (IBAction)clickAddShopCartBtn:(id)sender {

    self.addCartImg.hidden = false;
// 创建动画对象
CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
// 创建一个CGPathRef对象,就是动画的路线
CGMutablePathRef path = CGPathCreateMutable();
// 设置开始位置
CGPathMoveToPoint(path, NULL, self.addCartImg.layer.position.x-, self.addCartImg.layer.position.y-);//移动到起始点
// 沿着路径添加四曲线点移动
CGPathAddQuadCurveToPoint(path, NULL, , , self.view.frame.size.width, );
keyframeAnimation.path = path;
keyframeAnimation.delegate = self;
CGPathRelease(path);
keyframeAnimation.duration = 0.7;
[self.addCartImg.layer addAnimation:keyframeAnimation forKey:@"KCKeyframeAnimation_Position"]; // 旋转动画
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = 0.1;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = ; // 为addCartImg添加旋转动画
[self.addCartImg.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

点击按钮,addCartImg会做一个旋转操作,并按照规定的路径进行移动,从而完成一个动画!

ios开发之--CAKeyframeAnimation的详细用法的更多相关文章

  1. iOS开发~CocoaPods使用详细说明 分类: ios相关 2015-04-01 16:45 68人阅读 评论(0) 收藏

    iOS开发-CocoaPods使用详细说明 一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来 ...

  2. iOS开发~CocoaPods使用详细说明

    一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...

  3. iOS开发~CocoaPods使用详细说明【转】

    一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...

  4. ios 开发发布证书配置详细流程

    iOS证书配置实践 本文参考了: iOS证书配置指南:http://dev.umeng.com/push/ios/license-configuration-guide 写在前面: 团队开发证书的管理 ...

  5. iOS 开发-- enum与typeof enum用法

    一, 两者的用法 枚举类型定义用关键字enum标识,形式为: enum标识符 { 枚举数据表 }; enum用来定义一系列宏定义常量区别用,相当于一系列的#define ** **,当然它后面的标识符 ...

  6. IOS开发-CALayer和UIView详细汇总

    1.    CALayer和UIView之间的关系: 在iOS系统中,你能看得见摸得着的东西基本上都是UIView,比如UI控件.图标等等,都是UIView. 其实UIView之所以能显示在屏幕上,完 ...

  7. IOS开发 - TextField 控件详细

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...

  8. iOS开发--TableView详细解释

    -.建立 UITableView  DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];  [Data ...

  9. IOS开发基础知识--碎片42

    1:报thread 1:exc_bad_access(code=1,address=0x70********) 闪退 这种错误通常是内存管理的问题,一般是访问了已经释放的对象导致的,可以开启僵尸对象( ...

随机推荐

  1. 合并多个Excel文件

    这条分享来自百度经验https://jingyan.baidu.com/article/e6c8503cb6ed7ee54e1a1811.html

  2. codechef Row and Column Operations 题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  3. python MD5加密方法

    import hashlibhash = hashlib.md5()hash.update('admin')print hash.hexdigest()

  4. P2430 严酷的训练 题解

    题目背景 Lj的朋友WKY是一名神奇的少年,在同龄人之中有着极高的地位... 题目描述 他的老师老王对他的程序水平赞叹不已,于是下决心培养这名小子. 老王的训练方式很奇怪,他会一口气让WKY做很多道题 ...

  5. ROS教程0 环境配置

    1安装环境配置(桌面进入命令行) echo "source /opt/ros/indigo/setup.bash" >> ~/.bashrc source ~/.bas ...

  6. win10下乌龟git安装和使用(转)

    文章转自http://blog.csdn.net/jdsjlzx/article/details/51098588 一.安装git for windows 首先下载Git for windows客户端 ...

  7. 使用zip.js压缩文件和解压文件

    zip.js官方网站为:https://stuk.github.io/jszip/ 在此说明,下面的例子基本上来自官方示例,大家可以做参考,官方示例地址为:https://stuk.github.io ...

  8. WebSocket原理与实践(四)--生成数据帧

    WebSocket原理与实践(四)--生成数据帧 从服务器发往客户端的数据也是同样的数据帧,但是从服务器发送到客户端的数据帧不需要掩码的.我们自己需要去生成数据帧,解析数据帧的时候我们需要分片. 消息 ...

  9. Volume is already attached by pod default/nginx-deployment-86dfb99868-szpkd. Status Running

    1.部署WordPress - mysql ,想扩容,修改deployment,结果报错: MountVolume.SetUp failed for volume "pvc-e" ...

  10. ELF格式文件分析以及运用

    基于本文的一个实践<使用Python分析ELF文件优化Flash和Sram空间的案例>. 1.背景 ELF是Executable and Linkable Format缩写,其官方规范在& ...