美文网首页
iOS 虚线分割线

iOS 虚线分割线

作者: 倪大头 | 来源:发表于2019-11-30 16:41 被阅读0次

效果如下:


image.png

继承一个UIView,重写drawRect方法:

DashLineView.h:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface DashLineView : UIView

@end

NS_ASSUME_NONNULL_END

DashLineView.m:

#import "DashLineView.h"

@implementation DashLineView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor blackColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 虚线颜色
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    // 虚线宽度
    CGContextSetLineWidth(context, 5);
    // 绘制起点
    CGContextMoveToPoint(context, 0, 0);
    // 绘制终点
    CGContextAddLineToPoint(context, self.frame.origin.x + self.frame.size.width, 0);
    // 虚线间隔
    CGFloat array[] = {3, 4};
    CGContextSetLineDash(context, 0, array, 2);
    CGContextStrokePath(context);
}

@end

相关文章

网友评论

      本文标题:iOS 虚线分割线

      本文链接:https://www.haomeiwen.com/subject/utwowctx.html