使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形

步骤:

1、新建UIBezierPath对象bezierPath

2、新建CAShapeLayer对象caShapeLayer

3、将bezierPath的CGPath赋值给caShapeLayer的path,即caShapeLayer.path = bezierPath.CGPath

4、把caShapeLayer添加到某个显示该图形的layer中

下面的小例子是一个环形的progress代码,有具体的使用方法

.h文件:

[cpp] view plaincopy
  1. #import <QuartzCore/QuartzCore.h>
  2. #import <UIKit/UIKit.h>
  3. @interface KACircleProgressView : UIView {
  4. CAShapeLayer *_trackLayer;
  5. UIBezierPath *_trackPath;
  6. CAShapeLayer *_progressLayer;
  7. UIBezierPath *_progressPath;
  8. }
  9. @property (nonatomic, strong) UIColor *trackColor;
  10. @property (nonatomic, strong) UIColor *progressColor;
  11. @property (nonatomic) float progress;//0~1之间的数
  12. @property (nonatomic) float progressWidth;
  13. - (void)setProgress:(float)progress animated:(BOOL)animated;
  14. @end

.m文件

[cpp] view plaincopy
  1. #import "KACircleProgressView.h"
  2. @implementation KACircleProgressView
  3. - (id)initWithFrame:(CGRect)frame
  4. {
  5. self = [super initWithFrame:frame];
  6. if (self) {
  7. // Initialization code
  8. _trackLayer = [CAShapeLayer new];
  9. [self.layer addSublayer:_trackLayer];
  10. _trackLayer.fillColor = nil;
  11. _trackLayer.frame = self.bounds;
  12. _progressLayer = [CAShapeLayer new];
  13. [self.layer addSublayer:_progressLayer];
  14. _progressLayer.fillColor = nil;
  15. _progressLayer.lineCap = kCALineCapRound;
  16. _progressLayer.frame = self.bounds;
  17. //默认5
  18. self.progressWidth = 5;
  19. }
  20. return self;
  21. }
  22. - (void)setTrack
  23. {
  24. _trackPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(self.bounds.size.width - _progressWidth)/ 2 startAngle:0 endAngle:M_PI * 2 clockwise:YES];;
  25. _trackLayer.path = _trackPath.CGPath;
  26. }
  27. - (void)setProgress
  28. {
  29. _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];
  30. _progressLayer.path = _progressPath.CGPath;
  31. }
  32. - (void)setProgressWidth:(float)progressWidth
  33. {
  34. _progressWidth = progressWidth;
  35. _trackLayer.lineWidth = _progressWidth;
  36. _progressLayer.lineWidth = _progressWidth;
  37. [self setTrack];
  38. [self setProgress];
  39. }
  40. - (void)setTrackColor:(UIColor *)trackColor
  41. {
  42. _trackLayer.strokeColor = trackColor.CGColor;
  43. }
  44. - (void)setProgressColor:(UIColor *)progressColor
  45. {
  46. _progressLayer.strokeColor = progressColor.CGColor;
  47. }
  48. - (void)setProgress:(float)progress
  49. {
  50. _progress = progress;
  51. [self setProgress];
  52. }
  53. - (void)setProgress:(float)progress animated:(BOOL)animated
  54. {
  55. }
  56. /*
  57. // Only override drawRect: if you perform custom drawing.
  58. // An empty implementation adversely affects performance during animation.
  59. - (void)drawRect:(CGRect)rect
  60. {
  61. // Drawing code
  62. }
  63. */
  64. @end

使用:

[cpp] view plaincopy
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view, typically from a nib.
  5. KACircleProgressView *progress = [[KACircleProgressView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
  6. [self.view addSubview:progress];
  7. progress.trackColor = [UIColor blackColor];
  8. progress.progressColor = [UIColor orangeColor];
  9. progress.progress = .7;
  10. progress.progressWidth = 10;
  11. }

最后上一张效果图:

使用CAShapeLayer与UIBezierPath画出想要的图形相关推荐

  1. 请计算ARMA(2,2)的因果域,平稳域、可逆域,并画出相应区域的图形。

    请计算ARMA(2,2)的因果域,平稳域.可逆域,并画出相应区域的图形.

  2. iOS通过CAShapeLayer和UIBezierPath画环形进度条

    UIBezierPath可以绘制矢量路径,而CAShapeLayer是Layer的子类,可以在屏幕进行绘制,本文主要思想是:CAShapeLayer按照UIBezierPath的矢量路径进行绘制. 效 ...

  3. 使用CAShapeLayer与UIBezierPath画动态柱状图

    </pre><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: ...

  4. 怎样快速画出一个正方体_图形与几何---正方体

    一.概念描述 现代数学:正方体亦称立方体.它是一种正多面体.即棱长相等的长方体,因此亦称正六面体,如下图. 小学数学:2007年北京版教材第10册的第3页指出:长.宽.高都相等的长方体叫作正方体(也叫 ...

  5. Android绘制(一):来用shape绘出想要的图形吧!

    目录 前言 shape绘制 矩形 椭圆 线 环 用shape绘制SeekBar 最后 前言 在没有UI设计师的时候, 或者是想简单看下效果的时候, 用shape进行快速绘制是极好的! 官方文档. sh ...

  6. 用python画星座_一个可以画出星座(中文标识)的jupyter文档

    star-chart-CN 绘制星座(中文标识) This a simple jupyter file to plot star consterllation in Chinese. 这是一个可以画出 ...

  7. 【多媒体课件制作软件】Focusky教程 | 怎样画出水平线、垂直线?

    (Focusky动画演示大师简称"FS软件")在"图形"工具下找到"\",点击,按住shift键并按下鼠标向右拖动即可在画布上画出水平直线: ...

  8. 用C语言编程画出图形,C语言图形编程(六) -图形程序设计实例:零件图形的绘制...

    实例:一个零件图形的绘制 有一个零件图,如下: 对图3-1中的零件图形,如何根据它所标注的尺寸,按照适当的顺序有步聚地画出该图形,这首先要分析此零件图形的几何关系,了解构成这个图形各线段的性质,才能顺 ...

  9. iOS学习:CAShapeLayer与DrawRect对比,与UIBezierPath画图形

    使用CAShapeLayer有以下一些优点 渲染快速.CAShapeLayer使用了硬件加速,绘制同一图形会比用Core Graphics快很多. 高效使用内存.一个CAShapeLayer不需要像普 ...

最新文章

  1. SAP LSMW 事务代码HUPAST的录屏后台执行报错 - Runtime error RAISE_EXCEPTION has occurred - 之分析
  2. 面向人工智能的计算机体系结构
  3. php中的函数调简单 传入参数即可,php函数与传递参数的简单示例
  4. JsRender (js模板引擎)
  5. pycharm 修改成中文教程(图文简单记录)
  6. PP面向订单生产模式探讨
  7. CRM_REPORT_RF_CHECK_AUTHORITY call CRM_REPORT_RF_AUTH_OBJ_ORD_LP
  8. document.getElementsByName和document.getElementById用法
  9. android基础知识学习(1) TextView属性大全+单行显示长文本
  10. 获取程序进程并杀死该进程集
  11. kerberos验证_SQL Server中的服务主体名称和Kerberos身份验证概述
  12. 暑假集训-7.31总结
  13. pytorch模型加载测试_pytorch加载和保存模型
  14. mysql绘制er图教程_使用MySQLWorkBench绘制ER图
  15. docker 设置阿里云加速器
  16. 向量空间的基和维数例题_向量空间的基与维数.ppt
  17. 什么牌子的降噪耳机好用又实惠?平价好用的蓝牙耳机推荐
  18. FAST-LIO2代码解析(五)
  19. check your GPU Compute Capability| 2021 英伟达 GPU 的计算能力排名
  20. 定积分问题的区间再现公式应用

热门文章

  1. 打破AI算力瓶颈,华为升级“智能计算”重塑IT行业
  2. 华为AI战略完整披露!2款AI芯片首次曝光,拳打TPU,争锋英伟达
  3. NIPS放榜:谷歌占1/10,微软DeepMind打平,中国小哥4篇一作全球领先
  4. 终于承认!马斯克证实特斯拉在造AI芯片,而且是世上最好的AI硬件
  5. 201771010101 白玛次仁《面向对象程序设计(Java)》第十一周学习总结
  6. hdu 1693 Eat the Trees 插头dp
  7. 【二分】【线段树】hdu6070 Dirt Ratio
  8. hdu 5596 GTW likes gt
  9. pyqt5在mac os 安装
  10. 浅谈CIVIL 3D