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

代码如下:

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. [ADS]An installation support file could not be installed

    ADS:ARM Developer Suits 错误:An installation support file could not be installed 描述: 之前安装了一个不能用的ADS的版本 ...

  2. WPFのclipToBounds与maskToBounds的区别

    UIView.clipsToBounds : 让子 View 只显示父 View 的 Frame 部分,子视图超出frame的部分不显示,默认为NO,设置为YES就会把超出的部分裁掉: maskToB ...

  3. (14)Python类

  4. 从零上手Python关键代码

    来源 https://www.kesci.com/home/project/59e4331c4663f7655c499bc3

  5. 月球美容计划之最小生成树(MST)

    寒假学的两个算法,普里姆,克鲁斯卡尔最终弄明确了.能够发总结了 先说说普里姆,它的本质就是贪心.先从随意一个点開始,找到最短边,然后不断更新更新len数组,然后再选取最短边并标记经过的点,直到全部的点 ...

  6. centos7下安装docker(3.2创建镜像build)

    通过Dockerfile创建镜像 注:这个Dockerfile一开始真的不知道是在哪来的,还以为是在官网下载下来得(当然网上也有很多dockerfile的模板,参考:https://hub.docke ...

  7. python MD5加密方法

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

  8. Excel中IF函数的嵌套用法(多条件)

    Excel中IF函数的嵌套用法(多条件)   Excel中IF函数的嵌套用法(多条件)   函数格式:if(logical_test,value_if_true,value_if_false).其中: ...

  9. go标准库的学习-net/http

    参考:https://studygolang.com/pkgdoc 概念解释: request:用户请求的信息,用来解析用户的请求信息,包括post.get.cookie.url等信息 respons ...

  10. mysql数据库的test类型

    文章参考自 window系统参考:http://blog.sina.com.cn/s/blog_46f7bb6d0102vde3.html linux 参考:http://www.linuxeye.c ...