绘制虚线的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 ...
随机推荐
- linux 下 vi 编辑器 使用
命令模式(command mode).插入模式(Insert mode)和底行模式(last line mode) 1.进入插入模式 按「i」切换进入插入模式「insert mode」,按“i”进入插 ...
- Check类之TypeValidation
(1)Validator类的visitTypeApply()方法 实例1: class TestTypeVal<T extends InputStream>{ TestTypeVal< ...
- 用gdb理解C宏(#和##)
在Unix/Linux内核代码以及GNU libc源代码中,有两个C的宏被广泛使用. 例如: /* glibc-2.25/sysdeps/powerpc/powerpc64/sysdep.h */ # ...
- mysql8.0关闭log-bin功能
一.查看log-bin是否开启: mysql> show variables like 'log-bin'; 二.关闭log-bin: 在配置文件中的 [mysqld] 配置节中增加 skip- ...
- C语言经典面试题目(转的,不过写的的确好!)
第一部分:基本概念及其它问答题 1.关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被声明为静态的变量在这一 ...
- Eclipse空白包的显示和隐藏
Eclipse空白包的显示和隐藏 点击三角形, ,下拉 -> Customize View... -> Empty packages (勾选)
- i.mx6 Android5.1.1 System server
1. 概述: 1. Zygote进程是Android Java世界的开创者,所有的Java应用程序进程都由Zygote进程创建: 2. Zygote创建应用程序进程过程其实就是复制自身进程地址空间作为 ...
- Android studio应用导入源码错误This attribute must be localized
This attribute must be localized 产生原因: 多语言错误,源码中关于语言的显示不能直接赋值,而是需要通过xml来实现: 例如 <TextView android: ...
- [转]The NTLM Authentication Protocol and Security Support Provider
本文转自:http://davenport.sourceforge.net/ntlm.html#ntlmHttpAuthentication The NTLM Authentication Proto ...
- Orchard源码:缓存设计
概述 从缓存失效的几种方式开始了解Orchard缓存设计 1.设置失效时间 Func<int> retrieve = () => _cacheManager.Get("te ...