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

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

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Redis缓存穿透、缓存雪崩问题分析

    Redis缓存穿透、缓存雪崩问题分析把redis作为缓存使用已经是司空见惯,但是使用redis后也可能会碰到一系列的问题,尤其是数据量很大的时候,经典的几个问题如下:(一)缓存和数据库间数据一致性问题分布式环境下(单机就不用说了)非常容易出现缓存和数据库间的数据一致性问题,针对这一点的话,只能说,如果你的项目对缓存的要求是强一致性的,那么请不要使用缓存。我们只能采取合适的策略来降低缓存和数据库间数据不一致的概率,而无法保证两…

    2022年6月29日
    25
  • 编程helloworld代码_pycharm怎么编写python代码

    编程helloworld代码_pycharm怎么编写python代码1.什么是Pycharm?PyCharm是一种PythonIDE,其带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具。能够帮助我们在编写代码时提高效率。2.下载Pycharm网上提供的有专业版和教育版之分(windows下的)。网址:https://www.jetbrains.com/pycharm/download/#section=windows·专业版是收费的,功能更全面…

    2022年8月28日
    2
  • 填充图画图片_脂肪填充失败

    填充图画图片_脂肪填充失败图片处理-填充图片-numpy.padnp.pad()常用于深度学习中的数据预处理(例如用于图片处理中填充图片),可以将numpy数组按指定的方法填充成指定的形状。对一维数组的填充importnumpyasnparr1D=np.array([1,1,2,2,3,4])”’不同的填充方法”’print(‘constant:’+str…

    2022年8月13日
    2
  • Photoshop7.0教程(七)绘图和编辑路径

    Photoshop7.0教程(七)绘图和编辑路径

    2021年7月24日
    58
  • 【毕业项目】基于VUE开发的电商后台管理系统

    【毕业项目】基于VUE开发的电商后台管理系统摘要随着我国互联网普及率的提高,电子商务发展插上了腾飞的翅膀,一路高歌猛进。本后台系统旨在借助先进的计算机、快捷的网络以及庞大的云数据存储来帮助电商更加方便的统计电商数据。本系统以html、css、javascript作为开发语言。采用前后端分离思想,PC端使用Vue.js框架,服务端采用node.js作为开发平台,Webpack为静态模块打包器,Element-ui为UI组件,less为CSS预处理语言,ES6作为规范。PC端包含用户管理模块、权限管理模块、角色管理模块、商品管理模块、分类参数管理

    2022年6月13日
    38
  • bt云服务器地址,windows2008搭建bttracker服务器

    bt云服务器地址,windows2008搭建bttracker服务器在windows2008系统上搭建bttrackers服务器所需要用到的两个包下载地址:我这里所使用的是Python-2.3b2版本来运行BitTorrent1、安装Python并设置环境变量安装步骤省略,没有什么特殊的,一直下一步就好,默认是安装在C盘,我这里更改了安装路径,安装在了E:\Python23下计算机-属性-高级系统设置-环境变量-系统变量-Path编辑-在变量值后面加上;E:\P…

    2022年6月17日
    43

发表回复

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

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