iOS竖直虚线画法

项目中有画水平虚线和竖直虚线的需求,便在网上参考了别人的水平虚线画法,子类化了竖直虚线的DashLineView

DashLineView.h

@interface DashLineView : UIView
- (instancetype)initWithFrame:(CGRect)frame withLineLength:(NSInteger)lineLength withLineSpacing:(NSInteger)lineSpacing withLineColor:(UIColor *)lineColor;
@end

DashLineView.m

- (instancetype)initWithFrame:(CGRect)frame withLineLength:(NSInteger)lineLength withLineSpacing:(NSInteger)lineSpacing withLineColor:(UIColor *)lineColor{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _lineLength = lineLength;
        _lineSpacing = lineSpacing;
        _lineColor = lineColor;
        _height = frame.size.height;
    }
    return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context =UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextSetLineWidth(context,1);
    CGContextSetStrokeColorWithColor(context, _lineColor.CGColor);
    CGFloat lengths[] = {_lineLength,_lineSpacing};
    CGContextSetLineDash(context, 0, lengths,2);
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 0,_height);
    CGContextStrokePath(context);
    CGContextClosePath(context);
}
//竖向的虚线
DashLineView *verticalDashLine = [[DashLineView alloc] initWithFrame:CGRectMake(kScreen_Width/2, 75+15, 0.5, 25) withLineLength:6 withLineSpacing:3 withLineColor:[UIColor colorWithHexString:LineGrayColor]];
[self.contentView addSubview:verticalDashLine];

dashline.png

DEMO地址:https://github.com/zhangjiahuan8888/HHDashLine
更多源码请访问github:https://github.com/zhangjiahuan8888

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,269评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,527评论 2 45
  • 关注公众号【企鹅爸妈e家亲】,获取更多教育心理资讯 把学费拿来念书还是环游世界更合适? 为什么? -Questio...
    企鹅爸妈阅读 180评论 0 0
  • 稻草人和稻草人 抽烟的和留着齐刘海的稻草人 在阳光普照的光辉大道上来回望 每一个选择都那么模棱两可 高楼的缝隙中飘...
    皮糙肉薄阅读 217评论 0 2
  • 我从一个即将下雨的城市,来到另一个阴雨缠绵的城市。 背包拖着疲倦的我,在这个不知疲倦的城市游荡。 胡同...
    沐之啊沐之阅读 213评论 0 2