博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 购物车动画
阅读量:7256 次
发布时间:2019-06-29

本文共 3344 字,大约阅读时间需要 11 分钟。

代码地址如下:

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

图1

  • 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 购物车动画

代码地址如下:

http://www.demodashi.com/demo/11155.html

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

你可能感兴趣的文章
个人Web前端开发切图PS设置
查看>>
阿里云SLB(负载均衡)获取真实ip地址, log_format配置
查看>>
IE这回在css flex中扳回一局?
查看>>
Asp.Net Web Api 2 实现多文件打包并下载文件示例源码
查看>>
前端模版引擎选择指南
查看>>
事件绑定机制简单实现
查看>>
七牛云音视频新功能:音频支持AAC_HE
查看>>
让你完全理解base64是怎么回事
查看>>
Meteor的临时的存储:Session
查看>>
iptables规则备份和恢复、firewall的zone的操作、service的操作
查看>>
zabbix API 删除host
查看>>
redis 高级特性一
查看>>
李彦宏亲测“自动驾驶汽车”,Apollo(阿波罗)坐镇
查看>>
NAT原理与配置
查看>>
Linux字符模式下的“远程桌面共享”及屏幕录制
查看>>
详解linux系列之sendmail邮箱服务的安装及配置
查看>>
nagios一键安装脚本
查看>>
结构化编程的三重境界:见山是山:朴素的直观逻辑编程
查看>>
Visual Studio 在根目录下运行文件系统站点 [ Visual Studio | 文件系统 | WebDev.WebServer.EXE ]...
查看>>
leetCode 273. Integer to English Words 字符串 | Hard
查看>>