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


相关推荐

  • http 400报错

    http 400报错http400报错—springmvc相关:1.使用了json入参,传递给了对象,如果对象里的属性,如这里的Bonus是int类型,你传入了非int类型,这里就会报4002.使用了@RequestBody,然而信息头ContentType是非application/json,如:application/x-www-form-urlencoded,也会报400转载于:https:…

    2022年6月11日
    45
  • 网页设计中另人头疼的浏览器兼容问题

    网页设计中另人头疼的浏览器兼容问题

    2021年8月7日
    73
  • jupyter的代码能用pycharm运行吗_pycharm 安装教程

    jupyter的代码能用pycharm运行吗_pycharm 安装教程在Pycharm中安装及使用Jupyter(图文详解)文章目录在Pycharm中安装及使用Jupyter(图文详解)一、材料二、安装Jupyter三、配置Jupyter四、使用Jupyter1.使用Cell2.使用jupyterMarkdownPycharm更新了对Jupyter的功能支持,结合IntelliJ的自动补全代码,自动格式化代码,执行调试…

    2022年8月25日
    5
  • ftp常用命令详解_ospf生成路由表的过程

    ftp常用命令详解_ospf生成路由表的过程在window下按window+r可打开DOS命令窗口,然后就可以输入FTP命令了。1.登录FTP服务器方法一:直接输入ftp加ip地址ftp192.168.10.xxx方法二:直接输入ftp,进入ftp服务后输入open加ip地址open192.168.10.xxx当连接成功后会让你进行身份验证,在输入密码时屏幕上没有任何显示,不用管,直接…

    2022年9月21日
    1
  • 微商分销系统哪家好,要怎么做?

    微商分销系统哪家好,要怎么做?分销模式的本质是分享经济,wemall微商分销系统将传统商品高昂的推广费用让利给消费者,并抽取一定金额作为分销商的佣金。分销模式使消费者直接与商家沟通,增加用户粘性,促成品牌的口碑转化为流量,相比与零售,分销有着无可比拟的优势,这也使越来越多的微商分销系统涌现出来。微信封杀分…

    2022年5月17日
    34
  • 领域驱动实践总结(基本理论总结与分析V+架构分析与代码设计+具体应用设计分析)

    领域驱动实践总结(基本理论总结与分析V+架构分析与代码设计+具体应用设计分析)领域驱动实践总结一:基本理论总结与分析一、领域驱动设计两大设计:战略设计和战术设计二、理解和分析领域+子域+核心域+通用域+支撑域三、理解和分析界限上下文,定义领域边界四、理解和分析实体和值对象五、理解和分析聚合思想:聚合和聚合根六、理解很分析领域事件来解耦微服务…

    2022年4月26日
    33

发表回复

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

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