iOS 绘制虚线
2016-06-18 11:00
501 查看
因为最近遇到了绘制虚线的问题,所以把问题的解决办法记录下来。
我把这个方法封装了一个类,请看代码
DrawDottedLine.h文件
DrawDottedLine.m文件
我把这个方法封装了一个类,请看代码
DrawDottedLine.h文件
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface DrawDottedLine : NSObject // 对外提供画虚线的方法 + (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor; @end
DrawDottedLine.m文件
#import "DrawDottedLine.h" @implementation DrawDottedLine - (instancetype)init { self = [super init]; if (self) { } return self; } /* 参数说明 lineView:用来画虚线的视图 lineLength:虚线中实线的长度 lineSpacing:虚线中空白的长度 lineColor:虚线的颜色 */ // 划线的方法 + (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor { CAShapeLayer *shapeLayer = [CAShapeLayer layer]; [shapeLayer setBounds:lineView.bounds]; [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))]; [shapeLayer setFillColor:[UIColor clearColor].CGColor]; // 设置虚线颜色为blackColor [shapeLayer setStrokeColor:lineColor.CGColor]; // 设置虚线宽度 [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)]; [shapeLayer setLineJoin:kCALineJoinRound]; // 设置线宽,线间距 [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]]; // 设置路径 CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, 0, 0); CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0); [shapeLayer setPath:path]; CGPathRelease(path); // 把绘制好的虚线添加上来 [lineView.layer addSublayer:shapeLayer]; } @end
相关文章推荐
- iOS开发——多线程编程(GCD)
- iOS中 iOS10 权限崩溃问题 韩俊强的CSDN博客
- iOS中 iOS10 权限崩溃问题 韩俊强的CSDN博客
- IOS的用户数据本地保存
- iOS JSON转Model的库 MJExtension的基本使用指导
- iOS适配
- iOS 中单例的使用以及原理
- iOS---使用CAEmitterLayer制作发射粒子的特效
- iOS 项目源码
- iOS开发多线程篇—创建线程
- iOS 【Runtime 初理解】
- iOS 【快速集成QQ表情键盘】
- iOS 4种方法显示GIF
- 深入浅出-iOS函数式编程的实现 && 响应式编程概念
- IOS开发 图形绘制,绘制线条,矩形,和垂直和居中绘制文字
- IOS的代理模式
- iOS-分段控制器-基本概念
- iOS主线程与子线程
- iOS 3DES加解密
- iOS 编码(Base64) + 哈希摘要(MD5+SHA)