使用CAShapeLayer与UIBezierPath画出想要的图形「建议收藏」

使用CAShapeLayer与UIBezierPath画出想要的图形「建议收藏」怪哈哈,ACG游戏,ACG和谐区,足控福利,里番漫画,里番动漫,里番库,里番吧,本子库使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形步骤:1、新建UIBezierPath对象bezierPath2、新建CAShapeLayer对象caShapeLayer3、将bezierPa


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

怪哈哈,ACG游戏,ACG和谐区,足控福利,里番漫画,里番动漫,里番库,里番吧,本子库

使用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 plain
copy

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

.m文件


[cpp] 
view plain
copy

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

使用:

[cpp] 
view plain
copy

  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画出想要的图形「建议收藏」

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/172286.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • qt中qmake的详解

    1.首先,感性的认识是,qmake可以利用源文件(包括头文件h,实现文件cpp,qt的ui文件等等)生成各种不同类型的工程,工程需要的Makefile文件,可执行的与不可执行的,这取决于所用的模板(包括app、lib、subdirs、vcapp、vclib)。2.创建pro文件,添加各种源文件,还可以设定平台相关的不同源文件,设置各种规则,利用qmake命令生成工程。后面会介绍pro文件可

    2022年4月3日
  • zencart网店模板

    zencart网店模板帮朋友做了个zencart网店模板 香香礼品网:http://myliba.com/

    2022年7月27日
  • ES6 模板字符串用法

    ES6 模板字符串用法解决字符串拼接问题使用模板字符串,可以省去‘+’拼接的操作,反引号“之间的视为一个整体view:<pv-html=”getHtml()”></p>method:getHtml(){leth1=`<h1>这是一个h1元素内容</h1>`returnh1}结果:通过表达式拼接对象属性使用${}表达式可以直接拼接对象属性的值:letuser={

    2022年8月21日
  • vim 操作

    vim 操作

    2021年9月11日
  • 7-1 判断回文字符串

    7-1 判断回文字符串7-1判断回文字符串(15分)输入一个字符串,判断该字符串是否为回文。回文就是字符串中心对称,从左向右读和从右向左读的内容是一样的。输入格式:输入在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。输出格式:输出在第1行中输出字符串。如果它是回文字符串,在第2行中输出Yes,否则输出No。输入样例1:level输出样例1:levelYes输入样例2:1+2=2+1=输出样例2:1+2=2+1=No双指针法#include<s

    2022年5月29日
  • 变量放在堆还是栈_堆和栈的共同特点

    变量放在堆还是栈_堆和栈的共同特点内容会持续更新,有错误的地方欢迎指正,谢谢!什么是栈区,什么是堆区栈区(stack):由编译器自动分配释放,存放函数的参数值、局部变量的值等,内存的分配是连续的,类似于数组,当我们声明变量时,编译器会自动接着当前栈区的结尾来分配内存。堆区(heap):由程序员分配释放,若程序员不释放,程序结束时由OS回收,内存的分配不是连续的,类似于链表。变量存放的位置C/C++程…

    2022年11月1日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号