Core Animation之多种动画效果

左右滑动动画切换屏幕-demo(看效果) 这是一个博客中的一个demo,只供浏览运行效果。 博客:http://blog.youkuaiyun.com/qiantujava/article/details/22689297 立即下载

前面介绍了Core Animation基础知识,还有CALayer的简单使用,最终还是有要动画的滴,这里列出几个动画效果,参考下能加深对Core Animation的认识和理解

1、把图片移到右下角变小透明

使用CAAnimationGroup叠加动画效果,就是下面按钮《把图片移到右下角变小透明》描述的效果:

     、

上面三个图是动画的三个状态,实现代码如下:

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snaguosha.png"]];  
  5.     self.imageView.frame = CGRectMake(10, 10, 128, 192);  
  6.     [self.view addSubview:self.imageView];  
  7.  }  


  1. - (IBAction)tranAction:(id)sender {  
  2.     CGPoint fromPoint = self.imageView.center;  
  3.       
  4.     //路径曲线  
  5.     UIBezierPath *movePath = [UIBezierPath bezierPath];  
  6.     [movePath moveToPoint:fromPoint];  
  7.     CGPoint toPoint = CGPointMake(300, 460);  
  8.     [movePath addQuadCurveToPoint:toPoint  
  9.                      controlPoint:CGPointMake(300,0)];  
  10.     //关键帧  
  11.     CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];  
  12.     moveAnim.path = movePath.CGPath;  
  13.     moveAnim.removedOnCompletion = YES;  
  14.       
  15.     //旋转变化  
  16.     CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];  
  17.     scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];  
  18.     //x,y轴缩小到0.1,Z 轴不变  
  19.     scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];  
  20.     scaleAnim.removedOnCompletion = YES;  
  21.       
  22.     //透明度变化  
  23.     CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];  
  24.     opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];  
  25.     opacityAnim.toValue = [NSNumber numberWithFloat:0.1];  
  26.     opacityAnim.removedOnCompletion = YES;  
  27.       
  28.     //关键帧,旋转,透明度组合起来执行  
  29.     CAAnimationGroup *animGroup = [CAAnimationGroup animation];  
  30.     animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim,opacityAnim, nil];  
  31.     animGroup.duration = 1;  
  32.     [self.imageView.layer addAnimation:animGroup forKey:nil];  
  33. }  


代码解析:上面关键帧设置了动画的路径,scaleAnim设置了缩小,opacityAnim设置了透明度的变化。把三个动画组合到:animGroup。

在把animGroup添加到imageView.layer层的动画里。于是动画效果就有了。

2、旋转并向右移动

  1. - (IBAction)RightRotateAction:(id)sender {  
  2.     CGPoint fromPoint = self.imageView.center;  
  3.     UIBezierPath *movePath = [UIBezierPath bezierPath];  
  4.     [movePath moveToPoint:fromPoint];  
  5.     CGPoint toPoint = CGPointMake(fromPoint.x +100 , fromPoint.y ) ;  
  6.     [movePath addLineToPoint:toPoint];  
  7.       
  8.     CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];  
  9.     moveAnim.path = movePath.CGPath;  
  10.       
  11.     CABasicAnimation *TransformAnim = [CABasicAnimation animationWithKeyPath:@"transform"];  
  12.     TransformAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];  
  13.       
  14.     //沿Z轴旋转  
  15.     TransformAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,0,0,1)];  
  16.       
  17.     //沿Y轴旋转  
  18.   //   scaleAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,0,1.0,0)];  
  19.       
  20.     //沿X轴旋转  
  21. //     TransformAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,1.0,0,0)];  
  22.     TransformAnim.cumulative = YES;  
  23.     TransformAnim.duration =3;  
  24.     //旋转2遍,360度  
  25.     TransformAnim.repeatCount =2;  
  26.     self.imageView.center = toPoint;  
  27.     TransformAnim.removedOnCompletion = YES;  
  28.     CAAnimationGroup *animGroup = [CAAnimationGroup animation];  
  29.     animGroup.animations = [NSArray arrayWithObjects:moveAnim, TransformAnim, nil];  
  30.     animGroup.duration = 6;  
  31.       
  32.     [self.imageView.layer addAnimation:animGroup forKey:nil];  
  33. }  
代码解析:可能你没注意到, CATransform3DMakeRotation,这返回的是旋转的值。上面的动画效果里返回的是 CATransform3DMakeScale缩放的值。
向右移动是因为关键帧使用了路径为直线的路径。

3、旋转并消除边缘锯齿

  1. - (IBAction)Rotate360Action:(id)sender {  
  2.     //图片旋转360度  
  3.     CABasicAnimation *animation = [ CABasicAnimation  
  4.                                    animationWithKeyPath: @"transform" ];  
  5.     animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];  
  6.       
  7.     //围绕Z轴旋转,垂直与屏幕  
  8.     animation.toValue = [ NSValue valueWithCATransform3D:  
  9.                          CATransform3DMakeRotation(M_PI, 0, 0, 1.0) ];  
  10.     animation.duration = 3;  
  11.     //旋转效果累计,先转180度,接着再旋转180度,从而实现360旋转  
  12.     animation.cumulative = YES;  
  13.     animation.repeatCount = 2;  
  14.       
  15.     //在图片边缘添加一个像素的透明区域,去图片锯齿  
  16.     CGRect imageRrect = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height);  
  17.     UIGraphicsBeginImageContext(imageRrect.size);  
  18.     [self.imageView.image drawInRect:CGRectMake(1,1,self.imageView.frame.size.width-2,self.imageView.frame.size.height-2)];  
  19.     self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();  
  20.     UIGraphicsEndImageContext();  
  21.       
  22.     [self.imageView.layer addAnimation:animation forKey:nil];  
  23. }  
如果你仔细观察,会看到第二个动画里在旋转时,图片边缘是有锯齿的,如何消除呢? 在图片边缘添加一个像素的透明区域,去图片锯齿。

UIGraphicsBeginImageContext 开始图片内容

UIGraphicsGetImageFromCurrentImageContext 获取当前内容作为图片,

UIGraphicsEndImageContext结束。是和UIGraphicsBeginImageContext配套使用的。

4、吃豆人动画

这个有点复杂,首先说下实现的步骤:
  1. 画一个吃豆人开口的路径:pacmanOpenPath
  2. 画一个吃豆人闭口的路径:pacmanClosedPath
  3. 新建一个闭口的吃豆人头的层:shapeLayer
  4. 把开口和闭口路径设置成CABasicAnimation *chompAnimation动画的起点和终点,这样循环就能出现咬牙的动画了。
  5. 最后设置一个路径为关键帧,让吃豆人在这条路径上行动。
代码如下:
  1. - (void)animationInit  
  2. {  
  3.     self.view.backgroundColor = [UIColor blackColor];  
  4.       
  5.     CGFloat radius = 30.0f;  
  6.     CGFloat diameter = radius * 2;  
  7.     CGPoint arcCenter = CGPointMake(radius, radius);  
  8.     // Create a UIBezierPath for Pacman's open state  
  9.     pacmanOpenPath = [UIBezierPath bezierPathWithArcCenter:arcCenter  
  10.                                                     radius:radius  
  11.                                                 startAngle:DEGREES_TO_RADIANS(35)  
  12.                                                   endAngle:DEGREES_TO_RADIANS(315)  
  13.                                                  clockwise:YES];  
  14.       
  15.     [pacmanOpenPath addLineToPoint:arcCenter];  
  16.     [pacmanOpenPath closePath];  
  17.       
  18.     // Create a UIBezierPath for Pacman's close state  
  19.     pacmanClosedPath = [UIBezierPath bezierPathWithArcCenter:arcCenter  
  20.                                                       radius:radius  
  21.                                                   startAngle:DEGREES_TO_RADIANS(1)  
  22.                                                     endAngle:DEGREES_TO_RADIANS(359)  
  23.                                                    clockwise:YES];  
  24.     [pacmanClosedPath addLineToPoint:arcCenter];  
  25.     [pacmanClosedPath closePath];  
  26.       
  27.     // Create a CAShapeLayer for Pacman, fill with yellow  
  28.     shapeLayer = [CAShapeLayer layer];  
  29.     shapeLayer.fillColor = [UIColor yellowColor].CGColor;  
  30.     shapeLayer.path = pacmanClosedPath.CGPath;  
  31.     shapeLayer.strokeColor = [UIColor grayColor].CGColor;  
  32.     shapeLayer.lineWidth = 1.0f;  
  33.     shapeLayer.bounds = CGRectMake(0, 0, diameter, diameter);  
  34.     shapeLayer.position = CGPointMake(-40, -100);  
  35.     [self.view.layer addSublayer:shapeLayer];  
  36.       
  37.     SEL startSelector = @selector(startAnimation);  
  38.     UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:startSelector];  
  39.     [self.view addGestureRecognizer:recognizer];  
  40. }  
  1. - (void)startAnimation {  
  2.     // 创建咬牙动画  
  3.     CABasicAnimation *chompAnimation = [CABasicAnimation animationWithKeyPath:@"path"];  
  4.     chompAnimation.duration = 0.25;  
  5.     chompAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];  
  6.     chompAnimation.repeatCount = HUGE_VALF;  
  7.     chompAnimation.autoreverses = YES;  
  8.     // Animate between the two path values  
  9.     chompAnimation.fromValue = (id)pacmanClosedPath.CGPath;  
  10.     chompAnimation.toValue = (id)pacmanOpenPath.CGPath;  
  11.     [shapeLayer addAnimation:chompAnimation forKey:@"chompAnimation"];  
  12.       
  13.     // Create digital '2'-shaped path  
  14.       
  15.     UIBezierPath *path = [UIBezierPath bezierPath];  
  16.     [path moveToPoint:CGPointMake(0, 100)];  
  17.     [path addLineToPoint:CGPointMake(300, 100)];  
  18.     [path addLineToPoint:CGPointMake(300, 200)];  
  19.     [path addLineToPoint:CGPointMake(0, 200)];  
  20.     [path addLineToPoint:CGPointMake(0, 300)];  
  21.     [path addLineToPoint:CGPointMake(300, 300)];  
  22.       
  23.     CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];  
  24.     moveAnimation.path = path.CGPath;  
  25.     moveAnimation.duration = 8.0f;  
  26.     // Setting the rotation mode ensures Pacman's mouth is always forward.  This is a very convenient CA feature.  
  27.     moveAnimation.rotationMode = kCAAnimationRotateAuto;  
  28.     [shapeLayer addAnimation:moveAnimation forKey:@"moveAnimation"];  
  29. }  

 

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     [self animationInit];  
  5. }  

还需要添加一个宏:

#define DEGREES_TO_RADIANS(x) (3.14159265358979323846 * x / 180.0)    计算角度转换

添加了个手势,点一下屏幕,吃豆人就动起来了。效果:

本篇例子代码:代码

容芳志 (http://blog.youkuaiyun.com/totogo2010)

用Multisim复刻经典收音机:从零搭建超外差中波调幅接收机(附仿真源文件) 本文详细介绍了如何使用Multisim软件从零搭建超外差中波调幅接收机,复刻经典收音机的设计过程。通过模块化搭建、稳定性优化和增益分配等实战指南,帮助读者掌握超外差架构的核心技术,并附赠仿真源文件供实践参考。 阅读详情

相关推荐

VS2019 从零构建 OSG+OSGEarth+OSGQt 开发环境实战指南

本文提供了一份详细的实战指南,指导开发者如何在Windows系统下使用VS2019从零开始编译和配置OSG、OSGEarth与OSGQt开发环境。文章涵盖了从环境准备、源码获取、CMake配置、Visual Studio编译到最终测试的完整流程,并分享了关键的避坑技巧,旨在帮助三维GIS与可视化开发者高效搭建稳定、可控的专业开发平台。

weixin_28826961的博客 366

IOS开发之——CABasicAnimation(95),看完直接怼产品经理

四 CAAnimation继承结构 注意:图中的黑色虚线代表“继承”某个类,红色虚线代表“遵守”某个协议 说明: CAAnimation遵守CAMediaTiming,可以给动画设置执行时长 CAAnimationGroup:动画组(可以执行一系列动画) CABasicAnimation:单一组动画(formValue toValue) CAKeyframeAnimation:多组动画 五 示例 5.1 位移动画 代码 @property (nonatomic,weak) CALa

m0_64604466的博客 450

水环境综合管控平台完整方案

水环境综合管控平台项目(2026-2027)摘要 项目概况: 总投资5亿元的水环境综合管控平台项目,是国家数字政府与生态文明建设战略的重要实践。项目严格遵循2026年最新政策标准,整合物联网、大数据和AI技术,构建覆盖水质监测、污染溯源、生态保护等全流程的智能管控体系。 核心内容: 系统架构:采用五层设计(基础设施层至展示层),集成10大核心功能模块,包括水质动态监测、污染智能溯源和生态评估预警系统; 技术创新:运用2026年最新边缘计算和数字孪生技术,实现毫秒级水质异常响应和三维流域仿真; 实施规划:分7

随读手记 403

CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)

CABasicAnimation类的使用方式就是基本的关键帧动画。 所谓关键帧动画,就是将Layer的属性作为KeyPath来注册,指定动画的起始帧和结束帧,然后自动计算和实现中间的过渡动画的一种动画方式。 CABasicAnimation的基本使用顺序 1.引用QuartzCore.framework 将"QuartzCore.framework"这个库添加到项目中。并且在需要使

FLY的专栏 1462

Cursor+Apifox MCP Server实战:5分钟搞定API自动化测试用例生成

本文详细介绍了如何利用Cursor IDE与Apifox MCP Server,在5分钟内将API文档自动转化为高质量的自动化测试用例。通过配置MCP Server建立数据桥梁,开发者可直接用自然语言指令,让AI基于真实接口规范生成可立即执行的pytest测试代码,大幅提升接口测试效率,特别适合快节奏的SaaS项目。

ooo22的博客 533

OpenOCD入门到精通:第28章 综合实战:RISC-V 调试

RISC-V调试实战摘要 本文介绍了RISC-V调试规范及其在OpenOCD中的实现。主要内容包括: RISC-V Debug规范的两个版本(0.11和0.13)及其差异,OpenOCD通过自动检测DTMCS寄存器版本号选择对应驱动实现。 RISC-V调试架构的分层设计,包含DTM、Debug Module、Program Buffer等核心组件,以及三种内存访问方式(progbuf、sysbus、abstract)。 以GD32VF103为例的实战调试配置,包括硬件连接、OpenOCD配置文件编写和GDB

从事嵌入式linux android驱动开发,拥有全栈能力 282

iOS Animation讲解

CAAnimation:核心动画的基础类,不能直接使用,负责动画运行时间,速度控制,本身实现了CAMediaTiming协议 CAPropertyAnimation:属性动画的基类(通过属性进行动画设置,是可动画属性),不能直接使用 CAAnimationGroup:动画组,动画组是一种组合设计模式,可以通过动画组来进行所有动画行为的统一控制,组中所有动画效果可以并发执行, CATransi

liuzhiwei_051512的专栏 541

Autosar MCAL-S32K324 Fls配置-基于EB

Autosar MCAL Flash配置,基于S32K324,EB软件

赞的博客 1512

CAShapeLayer 与CABasicAnimation结合使用时strokeStart、strokeEnd 与fromValue、toValue的用法

因为本人在将CASharpLayer和CABasicAnimation结合使用的时候,遇到了对属性的组合产生的效果弄不清的情况,特意对它们的用法进行了总结。  1 keyPath = strokeStart  动画的fromValue = 0,toValue = 1      strokeEnd默认为1,strokeStart从 0 到 1 ,strokeStart = 0 时有一条完整的

u014745414的专栏 3824

高压控制单元与接触器 —— BMS电池的“高压大门”

高压接触器在BMS电池管理系统中扮演着“高压大门”的角色,负责控制电池包与整车高压母线的连接与隔离。接触器的工作原理基于线圈产生的磁力吸合接点,通过主触点接通高压电流路径,辅助触点检测状态,灭弧结构保护接点。预充电路的设计是为了避免直接连接大电容时产生的浪涌电流冲击,通过预充电阻逐步充电,确保系统安全上电。接触器控制策略包括顺序控制、辅助触点检测、温度监控、故障断电和背靠背MOS辅助控制,以确保系统的高效和安全运行。工程师在选择接触器型号、控制线圈、检测状态、软件冗余控制和预充失败保护等方面提供了具体建议。

weixin_43199439的博客 811

第二天 原来Labview语言结构也挺自然(2.1 顺序结构)

新建项目文件StructureTest.lvproj,新建四个子VI(FlatSequence.vi、CaseStructure.vi、LoopStructure.vi、EventStructure.vi),具体新建流程可以参考1.2节。

weixin_38637405的博客 1002

Core Animation基础介绍、简单使用CALayer以及多种动画效果

Core Animation iOS 转自荣芳志的博客: Core Animation之基础介绍  Core Animation可以翻译为核心动画,它为图形渲染和动画提供了基础。使用核心动画,你只需要设置一些参数比如起点和终点,剩下的帧核心动画为你自动完成。核心动画使用硬件加速,不用消耗cpu资源。其实平时咱们开发的iOS应用都在有意无意的使用了核心动画。

Wally1985的专栏 1351

【QNX+Android虚拟化方案】125 - 如何创建android-spare镜像

【QNX+Android虚拟化方案】125 - 如何创建android-spare镜像

|~~~热爱生活、努力学习的小伙汁~~~| 1088

IOS Core Animation之多种动画效果

出自: 前面介绍了Core Animation基础知识,还有CALayer的简单使用,最终还是有要动画的滴,这里列出几个动画效果,参考下能加深对Core Animation的认识和理解 1、把图片移到右下角变小透明 使用CAAnimationGroup叠加动画效果,就是下面按钮《把图片移到右下角变小透明》描述的效果:      、 上面三个图是动画的三个状态,实现代码

MoMo_KUA_1的专栏 893

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

上一篇: 为 Android* 设备构建动态 UI
下一篇: Core Animation之简单使用CALayer
focusjava
博客等级 码龄14年 127粉丝 528原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值