CAShapeLayer 学习和实践

2023-11-01 09:20
文章标签 学习 实践 cashapelayer

本文主要是介绍CAShapeLayer 学习和实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

介绍

CAShapeLayer是一个通过矢量图形而不是bitmap来绘制的图层子类。你指定诸如颜色和线宽等属性,用CGPath来定义想要绘制的图形,最后CAShapeLayer就自动渲染出来了。当然,你也可以用Core Graphics直接向原始的CALyer的内容中绘制一个路径,相比直下,使用CAShapeLayer有以下一些优点:
* 渲染快速。CAShapeLayer使用了硬件加速,绘制同一图形会比用Core Graphics快很多。
* 高效使用内存。一个CAShapeLayer不需要像普通CALayer一样创建一个寄宿图形,所以无论有多大,都不会占用太多的内存。
* 不会被图层边界剪裁掉。一个CAShapeLayer可以在边界之外绘制。你的图层路径不会像在使用Core Graphics的普通CALayer一样被剪裁掉(如我们在第二章所见)。
* 不会出现像素化。当你给CAShapeLayer做3D变换时,它不像一个有寄宿图的普通图层一样变得像素化。

其实个人理解就是比CALayer更自由,高度自定义化。

实践

现在我们要实现这样一个进度条,先看看效果。
圆形进度条.gif

思路
  1. 创建一个CAShapeLayer作为底部的圆环,暂且命名为:_trackLayer
  2. 创建一个CAShapeLayer作为进度条,暂且命名为:_progressLayer
  3. 创建一个动画来控制_progressLayer变化
@interface CircularProgressViewController ()
{CAShapeLayer *_trackLayer;  CAShapeLayer *_progressLayer;
}
- (void)viewDidAppear:(BOOL)animated
{[super viewDidAppear:animated];CGFloat ViewWith = CGRectGetWidth(self.view.frame);//获取中心点CGPoint position = self.view.center;//设置layer的path。需要注意的是:ArcCenter是针对下面layer你设置位置之后的中心点。//比如说: _trackLayer.bounds 设置的是(00200200)ArcCenter是(00)。那么圆会在_trackLayer的左上角,而不是当前页面的左上角。如果是(100100),那么圆就会出现在layer的中心点。这个很容易搞混//所以说,他的位置是相对于父视图的。UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(ViewWith / 2, ViewWith / 2) radius:ViewWith / 2 - 10 startAngle:- M_PI_2 endAngle:M_PI_2 * 3 clockwise:YES];_trackLayer = [CAShapeLayer layer];_trackLayer.bounds = CGRectMake(0, 0, ViewWith, ViewWith);_trackLayer.position = position;_trackLayer.lineCap = kCALineCapRound;  //线闭合时的样式_trackLayer.strokeColor = [UIColor greenColor].CGColor;  //轨迹颜色_trackLayer.fillColor = [UIColor clearColor].CGColor;  //空心颜色_trackLayer.lineWidth = 10;  //线的粗细_trackLayer.path = path.CGPath; [self.view.layer addSublayer:_trackLayer];_progressLayer = [CAShapeLayer layer];_progressLayer.bounds = CGRectMake(0, 0, ViewWith, ViewWith);_progressLayer.position = CGPointMake(CGRectGetWidth(_trackLayer.bounds) / 2, CGRectGetWidth(_trackLayer.bounds) / 2);_progressLayer.strokeColor = [UIColor redColor].CGColor;_progressLayer.fillColor = [UIColor clearColor].CGColor;_progressLayer.lineCap = kCALineCapRound;_progressLayer.lineWidth = 10;_progressLayer.path = path.CGPath;_progressLayer.strokeEnd = 0;[_trackLayer addSublayer:_progressLayer];
}

那么截止到这里,你应该在屏幕上的正中间看到一个绿色的轨迹了。那么我们设置的红色的进度条呢?对比一下_trackLayer和_progressLayer。_progressLayer我们多设置了一个strokeEnd属性。下面我们要针对这个属性处理动画。

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  // 展现动画的方式。使动画看起来更加逼真。
animation.duration = 2;  //动画时间
animation.fromValue = 0;  //动画起始值
animation.toValue = @1;  //动画目标值
animation.fillMode = kCAFillModeForwards;  //设置动画结束后依然显示结束后的状态
animation.removedOnCompletion = NO;  
[_progressLayer addAnimation:animation forKey:nil];

那么截止到这里一个完整的圆形进度条就做完了。

拓展一:实现一个彩色的进度条

彩色进度条.gif
将_progressLayer从当前view移除

[_progressLayer addAnimation:animation forKey:nil];

并在动画代码之前加上这段代码,就可以实现彩色的进度条。

CAGradientLayer *colorLayer = [CAGradientLayer layer];
colorLayer.frame = _progressLayer.frame;
[colorLayer setColors:@[(id)[UIColor blueColor].CGColor,(id)[UIColor yellowColor].CGColor,(id)[UIColor greenColor].CGColor]];
colorLayer.mask = _progressLayer;
[_trackLayer addSublayer:colorLayer];

拓展二:实现一个锦上添花的效果。

更为炫酷.gif
要实现上面图片的动画,则需要使用动画组。下面直接上代码。

CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation1.duration = 2;
animation1.fromValue = 0;
animation1.toValue = @1;
animation1.fillMode = kCAFillModeForwards;
animation1.removedOnCompletion = NO;CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
animation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation2.duration = 2;
animation2.fromValue = 0;
animation2.toValue = @0.25;
animation2.fillMode = kCAFillModeForwards;
animation2.removedOnCompletion = NO;CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
animation3.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation3.beginTime = 1.5;
animation3.duration = 1;
animation3.fromValue = @0.25;
animation3.toValue = @1;
animation3.fillMode = kCAFillModeForwards;
animation3.removedOnCompletion = NO;CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[animation1,animation2,animation3];
group.duration = 2.5;
group.repeatCount = MAXFLOAT;
[_progressLayer addAnimation:group forKey:nil];

这里面有个大坑就是动画效果一定要选择kCAMediaTimingFunctionEaseInEaseOut,划重点,必考。

总结

其实动画很简单,就是分解每一步动作。实现需要耐心和恒心。以上资料,我研究了三天。是难也好,笨也罢。坚持住吧。

源码:https://github.com/lpy834994897/AnimationSimple

这篇关于CAShapeLayer 学习和实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/322029

相关文章

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro