代码地址如下:
http://www.demodashi.com/demo/11155.html

先看看动画效果:

项目结构:

接下来开始具体实现过程:


一、先计算动画开始结束位置

方法:- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;

用该方法计算动画view相对于window的位置

1) 计算动画开始位置fromCenter

CGPoint fromCenter =  [animationView convertPoint:CGPointMake(animationView.frame.size.width * 0.5f, animationView.frame.size.height * 0.5f) toView:keyWindow];

2)计算动画结束位置endCenter

CGPoint endCenter = [endView convertPoint:CGPointMake(endView.frame.size.width * 0.5f, endView.frame.size.height * 0.5f) toView:keyWindow];

二、计算贝塞尔曲线(抛物线)的两个控制点

用UIBezierPath 画出抛物线,需要找到两个控制点controlPoint1 和 controlPoint2

  • controlPoint1是控制点1
  • controlPoint2是控制点2
  • AcontrolPoint1controlPoint2的中点
  • controlPointCfromCenterB的中点

1)先设置控制点距最高点(fromCenterendCenter)的水平距离controlPointEY,本篇默认controlPointEY = 100,即图1中点controlPointC到点A的距离。

2)计算控制点相对于点A的距离controlPointEX,即controlPoint1A距离或controlPoint2A距离,本篇设置为fromCenter.xendCenter.x的1/4,即controlPointEX = (endCenter.x - fromCenter.x) * 0.25f;

3)计算两个控制点位置

CGPoint controlPoint1 = CGPointMake(controlPointCX - controlPointEX, controlPointCY - controlPointEY);
CGPoint controlPoint2 = CGPointMake(controlPointCX + controlPointEX, controlPointCY - controlPointEY);

三、复制动画的layer

复制cell上需要做动画的view,add到window上做动画

NSString *str = ((UIButton *)animationView).titleLabel.text;
_animationLayer = [CATextLayer layer];
_animationLayer.bounds = animationView.bounds;
_animationLayer.position = fromCenter;
_animationLayer.alignmentMode = kCAAlignmentCenter;//文字对齐方式
_animationLayer.wrapped = YES;
_animationLayer.contentsScale = [UIScreen mainScreen].scale;
_animationLayer.string = str;
_animationLayer.backgroundColor = [UIColor redColor].CGColor;
[keyWindow.layer addSublayer:_animationLayer];

四、动画组合

整合所有动画,让view动起来

1)运动轨迹(抛物线)

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:fromCenter];
[path addCurveToPoint:endCenter controlPoint1:controlPoint1 controlPoint2:controlPoint2];
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.path = path.CGPath;

2)旋转起来

CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotateAnimation.removedOnCompletion = YES;
rotateAnimation.fromValue = [NSNumber numberWithFloat:0];
rotateAnimation.toValue = [NSNumber numberWithFloat:10 * M_PI];
rotateAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]

3)缩放动画

CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.removedOnCompletion = NO;
scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
scaleAnimation.toValue = [NSNumber numberWithFloat:0.2];

4)透明度动画

CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
alphaAnimation.removedOnCompletion = NO;
alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0];
alphaAnimation.toValue = [NSNumber numberWithFloat:0.1];

5)动画组合

CAAnimationGroup *groups = [CAAnimationGroup animation];
groups.animations = @[pathAnimation,rotateAnimation, scaleAnimation, alphaAnimation];
groups.duration = kShoppingCartDuration;
groups.removedOnCompletion=NO;
groups.fillMode=kCAFillModeForwards;
groups.delegate = self;
[_animationLayer addAnimation:groups forKey:@"group"];

五、其他

模拟贝塞尔曲线


参考:iOS 一分钟搞定加入购物车的交互动画

iOS 购物车动画

代码地址如下:
http://www.demodashi.com/demo/11155.html

注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权

iOS 购物车动画的更多相关文章

  1. iOS 手机淘宝加入购物车动画分析

    1.最终效果 仿淘宝动画 2.核心代码 _cartAnimView=[[UIImageView alloc] initWithFrame:CGRectMake(_propView.frame.size ...

  2. iOS手机淘宝加入购物车动画分析

    本文转载至 http://www.jianshu.com/p/e77e3ce8ee24 1.最终效果 仿淘宝动画 2.核心代码 _cartAnimView=[[UIImageView alloc] i ...

  3. ios 学习动画的套路 (一)

    你也肯定喜欢炫酷的动画! 在APP中,动画就是一个点睛之笔!可以给用户增加一些独特的体验感,估计也有许多的和我一样的,看着那些觉得不错的动画,也就只能流口水的孩子,毕竟~不知道从哪里下手去写!会连续的 ...

  4. iOS核心动画学习整理

    最近利用业余时间终于把iOS核心动画高级技巧(https://zsisme.gitbooks.io/ios-/content/chapter1/the-layer-tree.html)看完,对应其中一 ...

  5. IOS 核心动画之CAKeyframeAnimation - iBaby

    - IOS 核心动画之CAKeyframeAnimation - 简单介绍 是CApropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation ...

  6. javascript仿天猫加入购物车动画效果

    javascript仿天猫加入购物车动画效果   注意:首先需要声明的是:代码原思路不是我写的,是在网上找的这种效果,自己使用代码封装了下而已:代码中都有注释,我们最主要的是理解抛物线的思路及在工作中 ...

  7. iOS各种动画效果

    ios各种动画效果 最普通动画: //开始动画 [UIView beginAnimations:nil context:nil];  //设定动画持续时间 [UIView setAnimationDu ...

  8. 【转】Android 实现蘑菇街购物车动画效果

    原文出处:http://blog.csdn.net/wangjinyu501/article/details/38400479 1.思路   目前想到两种方式实现这种效果,一是使用Tween动画,直截 ...

  9. IOS之动画

    IOS之动画   15.1 动画介绍 15.2 Core Animation基础 15.3 隐式动画 15.4 显式动画 15.5 关键帧显式动画 15.6 UIView级别动画 15.1 动画介绍 ...

随机推荐

  1. webpack2 热加载js 文件

    如果只要普通的热加载 只要如下配置就好了 package.json { "devDependencies": { "webpack": "^2.6.1 ...

  2. [Codeforces #188] Tutorial

    Link: Codeoforces #188 传送门 A: 先全转为正数,后面就全是指数级增长了 #include <bits/stdc++.h> using namespace std; ...

  3. Luogu P3579 [POI2014]PAN-Solar Panels

    题目大意: 给出T组询问,每组询问给出四个数a,b,c,d,每次询问满足a<=x<=b,c<=y<=d的gcd(x,y)的最大值 首先可以想到如果存在gcd(x,y)=k,那么 ...

  4. 【计算几何】【分类讨论】Gym - 101243I - Land Division

    题意:给你一个n个点的凸包,让你切一刀,使得它变成一个m边形和一个K边形,问你切的这一刀最短是多少. 如果m+K==n+4,那么一定切在两条边上,但是由于两个线段间的最短距离,至少会经过一条线段的一个 ...

  5. 【FFT卷积】BZOJ3527-力

    [题目大意] [思路] 很好这很FFT…… 想了半天也没明白到底什么是卷积∑的上下界,我当初学的时候没说一定要从0开始啊quq 我还是背不出FFT的模板我要狗带了 我上面写的什么乱七八糟的,要什么数学 ...

  6. springmvc poi实现报表导出

    1.pom文件: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</ ...

  7. 《Windows驱动开发技术详解》之StartIO例程

    内容中包含 base64string 图片造成字符过多,拒绝显示

  8. 关于muse-ui 图片不显示的办法。

    直接使用框架提供的例子不能显示图片.需要npm安装 才可以直接使用. npm install --save material-design-icons

  9. EasyUI datagrid 复选框可以多选但不能全选功能实现

    1.功能需求:  实现多选,但是不能够全选功能 2.js代码 //帮卖列表页面,可以多选但是不能够全选实现 $(".datagrid-header-check").children ...

  10. 关于weblogic server对docker的支持

    Docker是目前比较热门的一个技术话题,WebLogic Server从12.1.3版本支持Docker,但对于操作系统和内核有一定的要求,具体参考下表 我曾在ubuntu,Oracle Enter ...