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

步骤:

1、新建UIBezierPath对象bezierPath

2、新建CAShapeLayer对象caShapeLayer

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

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

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

.h文件:

  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文件

  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

使用:

  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. 使用CAShapeLayer的path属性与UIBezierPath画出扫描框

    1.CAShapeLayer CAShapeLayer具有path属性,(是CGPath对象),可以使用这个属性与UIBezierPath画出想要的图形.该子类根据其fill color和stroke ...

  2. 使用CAShapeLayer和UIBezierPath画一个自定义半圆弧button

    通常我们使用系统自带的UIButton时,一般都是Rect矩形形式的,或则美工给出一张半圆弧的按钮,如图为一张半圆加三角形的按钮,而此时,如果给按钮添加点击事件时,响应事件依然为矩形区域,不符合我们的 ...

  3. 用css画出三角形

    看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...

  4. 如何用css画出三角形

    看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...

  5. 用css画出三角形【转】

    看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...

  6. CSS画出的各种形状图

    利用CSS可以画出各种需要的图形目录[1]矩形[2]圆形[3]椭圆[4]直角三角形[5]正三角形[6]平行四边形[7]梯形[8]六角星[9]六边形[10]五角星简单图形 矩形div{ width: 1 ...

  7. iOS关于CAShapeLayer与UIBezierPath的知识内容

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

  8. CAShapeLayer(UIBezierPath)、CAGradientLayer绘制动态小车

    看到一个大神写的代码,引用过来让大家看看! //  1.CAShapeLayer是一种特殊的层,可以在上面渲染图形. //  2.CAShapeLayer继承自CALayer,可使用CALayer的所 ...

  9. H5坦克大战之【画出坦克】

    今天是个特殊的日子,圣诞节,也是周末,在这里先祝大家圣诞快乐!喜庆的日子,我们可以稍微放松一下,扯一扯昨天雷霆对战凯尔特人的比赛,这场比赛大威少又双叒叕拿下三双,而且是一个45+11+11的超级三双, ...

随机推荐

  1. Demo学习: Basic jQuery

    UniGUI是一套基于ExtJS的Delphi的WEB框架,它是使用ExtPascal来转化到ExtJS,ExtJS是一个跨浏览器的JavaScript库,因此UniGUI发布出来的程序可以在各种浏览 ...

  2. WPF-控件-层级控件-Menu-嵌套结构

    <?xml version="1.0" encoding="utf-8" ?> <Data xmlns=""> &l ...

  3. Android中解析JSON形式的数据

    1.JSON(JavaScript Object Notation) 定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式, ...

  4. str_replace使用array替换

    <?php //替换采集等通过url参数传值 function admin_ff_url_repalce($xmlurl,$order='asc'){ if($order=='asc'){ re ...

  5. kendo ui template的用法

    kendo ui template的用法: Kendo UI 框架提供了一个易用,高性能的JavaScript模板引擎.通过模板可以创建一个HTML片段然后可以和JavaScript数据合并成最终的H ...

  6. std::function赋值的几种方法

    定义: #include <functional> std::function<void(const QString&)> myPrintFunction; 函数指针 ...

  7. bnuoj 33656 J. C.S.I.: P15(图形搜索题)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=33656 [题解]:暴力搜索题 [code]: #include <iostream> # ...

  8. Careercup - Facebook面试题 - 5412018236424192

    2014-05-01 01:32 题目链接 原题: Given a linked list where apart from the next pointer, every node also has ...

  9. Windows 10 响应式设计和设备友好的开发

    使用Effective pixels有效像素设计UI 什么是缩放像素和Effective有效像素: 当你的应用程序运行在Windows的设备,系统用一个算法控制的规范,字体,和其他UI元素显示在屏幕上 ...

  10. 用MSBuild和Jenkins搭建持续集成环境 - 转

    http://www.infoq.com/cn/articles/MSBuild-1 http://www.infoq.com/cn/articles/MSBuild-2 MSBuild是在.NET ...