使用CAShapeLayer与UIBezierPath画出想要的图形
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形
步骤:
1、新建UIBezierPath对象bezierPath
2、新建CAShapeLayer对象caShapeLayer
3、将bezierPath的CGPath赋值给caShapeLayer的path,即caShapeLayer.path = bezierPath.CGPath
4、把caShapeLayer添加到某个显示该图形的layer中
下面的小例子是一个环形的progress代码,有具体的使用方法
.h文件:
- #import <QuartzCore/QuartzCore.h>
- #import <UIKit/UIKit.h>
- @interface KACircleProgressView : UIView {
- CAShapeLayer *_trackLayer;
- UIBezierPath *_trackPath;
- CAShapeLayer *_progressLayer;
- UIBezierPath *_progressPath;
- }
- @property (nonatomic, strong) UIColor *trackColor;
- @property (nonatomic, strong) UIColor *progressColor;
- @property (nonatomic) float progress;//0~1之间的数
- @property (nonatomic) float progressWidth;
- - (void)setProgress:(float)progress animated:(BOOL)animated;
- @end
.m文件
- #import "KACircleProgressView.h"
- @implementation KACircleProgressView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- _trackLayer = [CAShapeLayer new];
- [self.layer addSublayer:_trackLayer];
- _trackLayer.fillColor = nil;
- _trackLayer.frame = self.bounds;
- _progressLayer = [CAShapeLayer new];
- [self.layer addSublayer:_progressLayer];
- _progressLayer.fillColor = nil;
- _progressLayer.lineCap = kCALineCapRound;
- _progressLayer.frame = self.bounds;
- //默认5
- self.progressWidth = 5;
- }
- return self;
- }
- - (void)setTrack
- {
- _trackPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(self.bounds.size.width - _progressWidth)/ 2 startAngle:0 endAngle:M_PI * 2 clockwise:YES];;
- _trackLayer.path = _trackPath.CGPath;
- }
- - (void)setProgress
- {
- _progressPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(self.bounds.size.width - _progressWidth)/ 2 startAngle:- M_PI_2 endAngle:(M_PI * 2) * _progress - M_PI_2 clockwise:YES];
- _progressLayer.path = _progressPath.CGPath;
- }
- - (void)setProgressWidth:(float)progressWidth
- {
- _progressWidth = progressWidth;
- _trackLayer.lineWidth = _progressWidth;
- _progressLayer.lineWidth = _progressWidth;
- [self setTrack];
- [self setProgress];
- }
- - (void)setTrackColor:(UIColor *)trackColor
- {
- _trackLayer.strokeColor = trackColor.CGColor;
- }
- - (void)setProgressColor:(UIColor *)progressColor
- {
- _progressLayer.strokeColor = progressColor.CGColor;
- }
- - (void)setProgress:(float)progress
- {
- _progress = progress;
- [self setProgress];
- }
- - (void)setProgress:(float)progress animated:(BOOL)animated
- {
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- // Drawing code
- }
- */
- @end
使用:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- KACircleProgressView *progress = [[KACircleProgressView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
- [self.view addSubview:progress];
- progress.trackColor = [UIColor blackColor];
- progress.progressColor = [UIColor orangeColor];
- progress.progress = .7;
- progress.progressWidth = 10;
- }
最后上一张效果图:
使用CAShapeLayer与UIBezierPath画出想要的图形相关推荐
- 请计算ARMA(2,2)的因果域,平稳域、可逆域,并画出相应区域的图形。
请计算ARMA(2,2)的因果域,平稳域.可逆域,并画出相应区域的图形.
- iOS通过CAShapeLayer和UIBezierPath画环形进度条
UIBezierPath可以绘制矢量路径,而CAShapeLayer是Layer的子类,可以在屏幕进行绘制,本文主要思想是:CAShapeLayer按照UIBezierPath的矢量路径进行绘制. 效 ...
- 使用CAShapeLayer与UIBezierPath画动态柱状图
</pre><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: ...
- 怎样快速画出一个正方体_图形与几何---正方体
一.概念描述 现代数学:正方体亦称立方体.它是一种正多面体.即棱长相等的长方体,因此亦称正六面体,如下图. 小学数学:2007年北京版教材第10册的第3页指出:长.宽.高都相等的长方体叫作正方体(也叫 ...
- Android绘制(一):来用shape绘出想要的图形吧!
目录 前言 shape绘制 矩形 椭圆 线 环 用shape绘制SeekBar 最后 前言 在没有UI设计师的时候, 或者是想简单看下效果的时候, 用shape进行快速绘制是极好的! 官方文档. sh ...
- 用python画星座_一个可以画出星座(中文标识)的jupyter文档
star-chart-CN 绘制星座(中文标识) This a simple jupyter file to plot star consterllation in Chinese. 这是一个可以画出 ...
- 【多媒体课件制作软件】Focusky教程 | 怎样画出水平线、垂直线?
(Focusky动画演示大师简称"FS软件")在"图形"工具下找到"\",点击,按住shift键并按下鼠标向右拖动即可在画布上画出水平直线: ...
- 用C语言编程画出图形,C语言图形编程(六) -图形程序设计实例:零件图形的绘制...
实例:一个零件图形的绘制 有一个零件图,如下: 对图3-1中的零件图形,如何根据它所标注的尺寸,按照适当的顺序有步聚地画出该图形,这首先要分析此零件图形的几何关系,了解构成这个图形各线段的性质,才能顺 ...
- iOS学习:CAShapeLayer与DrawRect对比,与UIBezierPath画图形
使用CAShapeLayer有以下一些优点 渲染快速.CAShapeLayer使用了硬件加速,绘制同一图形会比用Core Graphics快很多. 高效使用内存.一个CAShapeLayer不需要像普 ...
最新文章
- SAP LSMW 事务代码HUPAST的录屏后台执行报错 - Runtime error RAISE_EXCEPTION has occurred - 之分析
- 面向人工智能的计算机体系结构
- php中的函数调简单 传入参数即可,php函数与传递参数的简单示例
- JsRender (js模板引擎)
- pycharm 修改成中文教程(图文简单记录)
- PP面向订单生产模式探讨
- CRM_REPORT_RF_CHECK_AUTHORITY call CRM_REPORT_RF_AUTH_OBJ_ORD_LP
- document.getElementsByName和document.getElementById用法
- android基础知识学习(1) TextView属性大全+单行显示长文本
- 获取程序进程并杀死该进程集
- kerberos验证_SQL Server中的服务主体名称和Kerberos身份验证概述
- 暑假集训-7.31总结
- pytorch模型加载测试_pytorch加载和保存模型
- mysql绘制er图教程_使用MySQLWorkBench绘制ER图
- docker 设置阿里云加速器
- 向量空间的基和维数例题_向量空间的基与维数.ppt
- 什么牌子的降噪耳机好用又实惠?平价好用的蓝牙耳机推荐
- FAST-LIO2代码解析(五)
- check your GPU Compute Capability| 2021 英伟达 GPU 的计算能力排名
- 定积分问题的区间再现公式应用
热门文章
- 打破AI算力瓶颈,华为升级“智能计算”重塑IT行业
- 华为AI战略完整披露!2款AI芯片首次曝光,拳打TPU,争锋英伟达
- NIPS放榜:谷歌占1/10,微软DeepMind打平,中国小哥4篇一作全球领先
- 终于承认!马斯克证实特斯拉在造AI芯片,而且是世上最好的AI硬件
- 201771010101 白玛次仁《面向对象程序设计(Java)》第十一周学习总结
- hdu 1693 Eat the Trees 插头dp
- 【二分】【线段树】hdu6070 Dirt Ratio
- hdu 5596 GTW likes gt
- pyqt5在mac os 安装
- 浅谈CIVIL 3D