使用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. [大牛翻译系列]Hadoop(3)MapReduce 连接:半连接(Semi-join)

    4.1.3 半连接(Semi-join) 假设一个场景,需要连接两个很大的数据集,例如,用户日志和OLTP的用户数据.任何一个数据集都不是足够小到可以缓存在map作业的内存中.这样看来,似乎就不能使用 ...

  2. 通过百度地图API实现搜索地址--第三方开源--百度地图(三)

    搜索地址功能是建立在能够通过百度地图API获取位置的基础上 通过百度地图定位获取位置详情:http://www.cnblogs.com/zzw1994/p/5008134.html package c ...

  3. 禁用cookie后

    服务器为某个访问者创建一个内存区域,这个就是所谓的session,这个区域的存在是有时间限制的,比如30分钟,这块区域诞生的时候,服务器会给这个区域分配一个钥匙,只有使用这个钥匙才能访问这个区域,这个 ...

  4. python_day2_homework_1(简单购物商城)

    '''简单购物商城(要求):1,商品展示,价格2,买,加入购物车3,付款,钱不够''' 1 #_*_ coding: utf-8 _*_ __author__ = 'A-rno' meu_list_1 ...

  5. 从零开始学ios开发(十四):Navigation Controllers and Table Views(上)

    这一篇我们将学习一个新的控件Navigation Controller,很多时候Navigation Controller是和Table View紧密结合在一起的,因此在学习Navigation Co ...

  6. windows下设置socket的connect超时

    SOCKET Open(const char* strIP, UINT nPort, int nTimeOut)    {        SOCKET sockfd = NULL;           ...

  7. C#的winform小合集

    C#的winform小合集 博主很懒,又想记录一下自己的所做所为,仅此而已,供自己日后所看.这个是博主自主学习C#所写的一些小程序,有好玩的,也有一些无聊闲得蛋疼所作的. 内容介绍 C#入门窗口输出h ...

  8. SNV ConnerStore使用说明

    1.  上传自己新建的文件新建的类文件 后面的 会有A标示要先 Add To Working copy 再点击提交 2. 上传第三方库时 默认SVN是忽略.a文件的要找到.a文件把他设置成不是忽略的通 ...

  9. Daily Scrum 11.11

    摘要:本次会议继续讨论程序的问题以及单元测试和集成测试,本次测试为1.02版本.本次的Task列表如下: Task列表 出席人员 Today's Task Tomorrow's Task 刘昊岩  t ...

  10. c语言编程之队列(链表实现)

    用链表实现了队列,完成了队列的入队和出队功能. #include"stdio.h" typedef int element; typedef struct Node{ struct ...