绘制虚线的UIView
绘制虚线的UIView

CAShapeLayer配合贝塞尔曲线可以绘制曲线,笔者继承了一个UIView的子类,并将该子类的backedLayer替换为CAShapeLayer,以此来实现绘制虚线的效果.
绘制出各种虚线的效果图:

实现的源码:
LineDashView.h 与 LineDashView.m
//
// LineDashView.h
// DASH
//
// 绘制虚线用的View
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface LineDashView : UIView @property (nonatomic, strong) NSArray *lineDashPattern; // 线段分割模式
@property (nonatomic, assign) CGFloat endOffset; // 取值在 0.001 --> 0.499 之间 - (instancetype)initWithFrame:(CGRect)frame
lineDashPattern:(NSArray *)lineDashPattern
endOffset:(CGFloat)endOffset; @end
//
// LineDashView.m
// DASH
//
// 绘制虚线用的View
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "LineDashView.h" @interface LineDashView () {
CAShapeLayer *_shapeLayer;
} @end @implementation LineDashView - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
_shapeLayer = (CAShapeLayer *)self.layer;
_shapeLayer.fillColor = [UIColor clearColor].CGColor;
_shapeLayer.strokeStart = 0.001;
_shapeLayer.strokeEnd = 0.499;
_shapeLayer.lineWidth = frame.size.height;
_shapeLayer.path = path.CGPath;
}
return self;
} - (instancetype)initWithFrame:(CGRect)frame
lineDashPattern:(NSArray *)lineDashPattern
endOffset:(CGFloat)endOffset
{
LineDashView *lineDashView = [[LineDashView alloc] initWithFrame:frame];
lineDashView.lineDashPattern = lineDashPattern;
lineDashView.endOffset = endOffset; return lineDashView;
} #pragma mark - 修改view的backedLayer为CAShapeLayer
+ (Class)layerClass
{
return [CAShapeLayer class];
} #pragma mark - 改写属性的getter,setter方法
@synthesize lineDashPattern = _lineDashPattern;
- (void)setLineDashPattern:(NSArray *)lineDashPattern
{
_lineDashPattern = lineDashPattern;
_shapeLayer.lineDashPattern = lineDashPattern;
}
- (NSArray *)lineDashPattern
{
return _lineDashPattern;
} @synthesize endOffset = _endOffset;
- (void)setEndOffset:(CGFloat)endOffset
{
_endOffset = endOffset;
if (endOffset < 0.499 && endOffset > 0.001)
{
_shapeLayer.strokeEnd = _endOffset;
}
}
- (CGFloat)endOffset
{
return _endOffset;
} #pragma mark - 重写了系统的backgroundColor属性
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
_shapeLayer.strokeColor = backgroundColor.CGColor;
}
- (UIColor *)backgroundColor
{
return [UIColor colorWithCGColor:_shapeLayer.strokeColor];
} @end
使用源码:
//
// RootViewController.m
// DASH
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import "LineDashView.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor]; // 线条宽度
CGFloat lineHeight = ; // 线条1
LineDashView *line1 = [[LineDashView alloc] initWithFrame:CGRectMake(, , , lineHeight)
lineDashPattern:@[@, @]
endOffset:0.495];
line1.backgroundColor = [UIColor redColor];
[self.view addSubview:line1]; // 线条2
LineDashView *line2 = [[LineDashView alloc] initWithFrame:CGRectMake(, , , lineHeight)
lineDashPattern:@[@, @]
endOffset:0.495];
line2.backgroundColor = [UIColor redColor];
[self.view addSubview:line2]; // 线条3
LineDashView *line3 = [[LineDashView alloc] initWithFrame:CGRectMake(, , , lineHeight)
lineDashPattern:@[@, @, @, @]
endOffset:0.495];
line3.backgroundColor = [UIColor redColor];
[self.view addSubview:line3]; // 线条4
LineDashView *line4 = [[LineDashView alloc] initWithFrame:CGRectMake(, , , lineHeight)
lineDashPattern:@[@, @, @, @, @, @]
endOffset:0.495];
line4.backgroundColor = [UIColor redColor];
[self.view addSubview:line4];
} @end
需要注意的地方:

修改了UIView的backedLayer

重写了两个属性的setter方法

不过,你也可以解放限制,实现更高端用法哦,不过你需要了解下CAShapeLayer的相关内容才能进行改写.

绘制虚线的UIView的更多相关文章
- iOS 使用drawRect: 绘制虚线椭圆
iOS 使用drawRect: 绘制虚线椭圆 1:首先如果要使用 drawRect 绘图 要导入 CoreGraphics.framework 框架 然后 创建 自定义view, 即是 myView继 ...
- iOS 学习 - 16.绘制虚线
//绘制虚线 -(void)set{ UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )]; [ ...
- UIKit和Core Graphics绘图(三)——绘制虚线,椭圆以及饼图
绘制虚线 虚线绘制主要调用CGContextSetLineDash函数. 这个函数有4个参数,除了一个是上下文外,phase为初始跳过几个点开始绘制,第三个参数为一个CGFloat数组,指定你绘制的样 ...
- canvas学习总结三:绘制虚线
上一章节我们说到,线性路径的绘制,主要利用movoTo(),lineTo()等方法,当然 Canvas 2D API 也提供了虚线的绘制方法,CanvasRenderingContext2D.setL ...
- 【Android使用Shape绘制虚线,在4.0以上的手机显示实线】解决方式
问题描写叙述: 用下面代码绘制虚线: <span style="font-family:Comic Sans MS;font-size:18px;"><? xml ...
- canvas学习总结四:绘制虚线
上一章节我们说到,线性路径的绘制,主要利用movoTo(),lineTo()等方法,当然 Canvas 2D API 也提供了虚线的绘制方法,CanvasRenderingContext2D.setL ...
- 【转】Android Shape绘制虚线在手机端查看是实线的问题
Android share绘制虚线在手机上显示实线问题 给控件添加Drawableleft等图片后,单独给图片设置动画效果,参考文章: http://blog.csdn.net/langzxz/art ...
- CAD交互绘制虚线(网页版)
用户可以在CAD控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE ...
- CAD交互绘制虚线(com接口)
用户可以在控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE dY ...
随机推荐
- JS写游戏
最近在看萧井陌的视频.感觉一些东西挺有意思的,尤其是解决问题的过程,以及一个好程序应该改进的地方. 萧大的GITHUB:github.com/guaxiao/gua.game.js 视频:https: ...
- 安装Ubunutu音频视频库
sudo apt-get install ubuntu-restricted-extras
- Install Papirus Icon Theme on Ubuntu
sudo add-apt-repository ppa:papirus/papirus sudo apt update && sudo apt install papirus-icon ...
- springboot+zuul(二)------智能负载
一.参考 参考资料:https://www.cnblogs.com/flying607/p/8330551.html ribbon+spring retry重试策略源码分析:https://blog. ...
- Difference between $.ajax() and $.get() and $.load()
转自:Difference between $.ajax() and $.get() and $.load() $.ajax() is the most configurable one, where ...
- iview中使用Tag时进行数据的变化和实现将输入内容转化为标签输出数组
上代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...
- JAR,WAR,EAR的使用与区别
WAR(Web Archive file)网络应用程序文件 是与平台无关的文件格式,它允许将许多文件组合成一个压缩文件.为 J2EE 应用程序创建的 JAR 文件是 EAR 文件(企业 JAR 文 ...
- vue2.0读书笔记1-基础
一.概述 二.模版语法 三.计算属性 四.class与style绑定 五.条件渲染 六.列表渲染 七.事件处理器 八.表单控件绑定 九.组件 一.概述 在底层的实现上, Vue 将模板编译成虚 ...
- oracle:ORA-00911: 无效字符 问题和解决---Eclipse中的SQL语句不能加分号
eclipse中原sql: 异常: 原因:Eclipse中的SQL语句不能加分号 去掉分号,正常执行,插入成功. 这里把id设为了主键,具有唯一性,重复插入同一id执行插入失败,ORA-00001号错 ...
- 使用FileSystemWatcher监视指定目录
使用 FileSystemWatcher 监视指定目录中的更改.可监视指定目录中的文件或子目录的更改. 以下是一个简单的实例,用来监控指定目录下文件的新增.删除.重命名等情况(文件内容更改会触发多次, ...