博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
绘制虚线的UIView
阅读量:6279 次
发布时间:2019-06-22

本文共 4365 字,大约阅读时间需要 14 分钟。

绘制虚线的UIView

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

绘制出各种虚线的效果图:

实现的源码:

LineDashView.h 与 LineDashView.m

////  LineDashView.h//  DASH////  绘制虚线用的View//  Copyright (c) 2014年 Y.X. All rights reserved.//#import 
@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;        // 线条1    LineDashView *line1 = [[LineDashView alloc] initWithFrame:CGRectMake(0, 100, 320, lineHeight)                                              lineDashPattern:@[@10, @10]                                                    endOffset:0.495];    line1.backgroundColor = [UIColor redColor];    [self.view addSubview:line1];        // 线条2    LineDashView *line2 = [[LineDashView alloc] initWithFrame:CGRectMake(0, 110, 320, lineHeight)                                              lineDashPattern:@[@5, @5]                                                    endOffset:0.495];    line2.backgroundColor = [UIColor redColor];    [self.view addSubview:line2];        // 线条3    LineDashView *line3 = [[LineDashView alloc] initWithFrame:CGRectMake(0, 120, 320, lineHeight)                                              lineDashPattern:@[@10, @5, @20, @10]                                                    endOffset:0.495];    line3.backgroundColor = [UIColor redColor];    [self.view addSubview:line3];        // 线条4    LineDashView *line4 = [[LineDashView alloc] initWithFrame:CGRectMake(0, 130, 320, lineHeight)                                              lineDashPattern:@[@10, @5, @20, @10, @30, @20]                                                    endOffset:0.495];    line4.backgroundColor = [UIColor redColor];    [self.view addSubview:line4];}@end

需要注意的地方:

修改了UIView的backedLayer

重写了两个属性的setter方法

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

转载地址:http://hjbva.baihongyu.com/

你可能感兴趣的文章
Docker容器启动报WARNING: IPv4 forwarding is disabled. Networking will not work
查看>>
(转)第三方支付参与者
查看>>
程序员修炼之道读后感2
查看>>
DWR实现服务器向客户端推送消息
查看>>
js中forEach的用法
查看>>
Docker之功能汇总
查看>>
!!a标签和button按钮只允许点击一次,防止重复提交
查看>>
(轉貼) Eclipse + CDT + MinGW 安裝方法 (C/C++) (gcc) (g++) (OS) (Windows)
查看>>
还原数据库
查看>>
作业调度框架 Quartz.NET 2.0 beta 发布
查看>>
mysql性能的检查和调优方法
查看>>
项目管理中的导向性
查看>>
Android WebView 学习
查看>>
(转)从给定的文本中,查找其中最长的重复子字符串的问题
查看>>
HDU 2159
查看>>
spring batch中用到的表
查看>>
资源文件夹res/raw和assets的使用
查看>>
UINode扩展
查看>>
LINUX常用命令
查看>>
百度云盘demo
查看>>