Core Animation之多种动画效果

左右滑动动画切换屏幕-demo(看效果) 这是一个博客中的一个demo,只供浏览运行效果。 博客:http://blog.csdn.net/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.csdn.net/totogo2010)

OGG工具实现MySQL同步到Oracle数据库 OGG工具实现MySQL同步数据到Oracle 阅读详情

相关推荐

单细胞转录组分析(scRNA-seq)

单细胞转录组测序(Single-cell RNA sequencing,scRNA-seq)是一种高通量测序技术,可以在单细胞水平解析基因表达情况,揭示细胞异质性,探索稀有细胞类型,并研究细胞状态转换及动态调控机制。与传统的。

大甘的博客 6159

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

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

m0_64604466的博客 451

基于STM32F427的模拟SSI协议绝对式编码器位置反馈通信

基于STM32F427的模拟SSI协议绝对式编码器位置反馈通信

xeke997的博客 6077

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

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

FLY的专栏 1463

【乐鑫】唤醒词修改方法

乐鑫科技的语音识别模块(如ESP-SR)支持自定义唤醒词,但需通过官方工具链完成。

以实战经验介绍行业知识 834

信奥赛C++提高组csp-s之数论基础专题课:从同余到分数模运算1(知识地图:同余、裴蜀定理、扩展欧几里得、乘法逆元、分数模运算)

信奥赛C++提高组csp-s之数论基础专题课:从同余到分数模运算1(知识地图:同余、裴蜀定理、扩展欧几里得、乘法逆元、分数模运算)

王老师青少年编程 794

iOS Animation讲解

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

liuzhiwei_051512的专栏 543

Coze智能体开发:扣子空间制作PPT功能

扣子空间内置了丰富的场景模板,涵盖人文历史、学术分享、研究报告、数据分析等场景,可以满足不同用户的需求。 你只需输入与制作 PPT 相关的提示词,Agent 就能基于这些提示词展示思考的过程,并自动生成完整的 PPT。

王国平的博客 2061

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

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

u014745414的专栏 3825

管好 SAP Fiori Launchpad Content:技术目录、业务目录与 KPI 工具的完整治理思路

SAP Fiori Launchpad 作为企业级统一入口,需通过技术目录(源对象)和业务目录(引用编排)分层治理。技术目录由 Launchpad App Manager 维护,负责应用描述对象(Tile 和 Target Mapping)的创建与管理;业务目录由 Launchpad Content Manager 编排,通过引用技术目录对象实现灵活配置。分析型内容则通过 F2814 统一管理。这种分层设计支持应用复用,便于升级维护,是 SAP S/4HANA 项目中实现高效内容治理的关键。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 144

unity从3D升级到urp管线,打包ab包后,材质全部变紫色

确保材质中的被转换为 URP 兼容的着色器。在打包 AssetBundle 时,确保所需的着色器被正确打包。确保运行 AB 包的目标项目中正确配置了 URP 渲染管线。如果加载后材质丢失,检查 AB 包的加载逻辑,确保材质正确应用了着色器。通过这些步骤,你应该能够解决从标准渲染管线切换到 URP 渲染管线后,材质在 AB 包中变紫色的问题。如果问题仍然存在,请根据具体错误日志进一步排查。希望如上措施及解决方案能够帮到有需要的你。

**My Coding Family** 1970

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

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

Wally1985的专栏 1351

YOLOv11改进 | 添加注意力篇 | 一文带你改进GAM、CBAM、CA、ECA等通道注意力机制和多头注意力机制

这篇文章给大家带来的改进机制是一个汇总篇,包含一些简单的注意力机制,本来一直不想发这些内容的(网上教程太多了,发出来增加文章数量也没什么意义),但是群内的读者很多都问我这些机制所以单独出一期视频来汇总一些比较简单的注意力机制添加的方法和使用教程,本文的内容不会过度的去解释原理,更多的是从从代码的使用上和实用的角度出发去写这篇教程。欢迎大家订阅我的专栏一起学习YOLO!YOLOv11改进系列专栏——本专栏持续复习各种顶会内容——科研必备目录一、本文介绍二、GAM2.1 GAM的介绍2.2 GAM的核心代码。

Snu77的博客 5160

IOS Core Animation之多种动画效果

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

MoMo_KUA_1的专栏 894

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

上一篇: 为 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、付费专栏及课程。

余额充值
⌂ 换网址