HJDashLineView.h
//
// HJDashLineView.h
// CGContextDemo
//
// Created by 黄健 on 16/7/18.
// Copyright © 2016年 黄健. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef enum : NSUInteger {
HJLineTypeDashLine,
HJLineTypeStraightLine
} HJLineType;
IB_DESIGNABLE
@interface HJDashLineView : UIView
// 如果不指定 lineType,默认绘制虚线,否则为实线
@property (nonatomic, assign) IBInspectable BOOL lineType;
/**
线条宽度 lineWidth 默认 1
线条颜色 lineColor 默认 blackColor
偏移点数 movePoint 默认 0
绘制点数 drawPoint 默认 5
跳过点数 stepPoint 默认 3
当视图宽大于高,水平虚线
当视图高大于宽,竖直虚线
当视图宽高相等,竖直虚线
*/
@property (nonatomic, assign) IBInspectable CGFloat lineWidth;
@property (nonatomic, strong) IBInspectable UIColor *lineColor;
@property (nonatomic, assign) IBInspectable CGFloat movePoint;
@property (nonatomic, assign) IBInspectable CGFloat drawPoint;
@property (nonatomic, assign) IBInspectable CGFloat stepPoint;
@end
HJDashLineView.m
//
// HJDashLineView.m
// CGContextDemo
//
// Created by 黄健 on 16/7/18.
// Copyright © 2016年 黄健. All rights reserved.
//
#import "HJDashLineView.h"
@implementation HJDashLineView
- (void)layoutSubviews
{
[super layoutSubviews];
// 由于设置的是直线或虚线,直接设置背景颜色为透明
self.backgroundColor = [UIColor clearColor];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
}
return self;
}
-(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGFloat width = self.bounds.size.width;
CGFloat height = self.bounds.size.height;
if (width > height)
{
CGPathMoveToPoint(path, nil, 0, height / 2.f);
CGPathAddLineToPoint(path, nil, width, height / 2.f);
}
else
{
CGPathMoveToPoint(path, nil, width / 2.f, 0);
CGPathAddLineToPoint(path, nil, width / 2.f, height);
}
CGContextAddPath(context, path);
CGContextSetStrokeColorWithColor(context, self.lineColor ? self.lineColor.CGColor : [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, self.lineWidth == 0 ? 1 : self.lineWidth);
if (self.lineType == HJLineTypeDashLine)
{
CGFloat lengths[2] = {self.drawPoint == 0 ? 5 : self.drawPoint, self.stepPoint == 0 ? 3 : self.stepPoint};
CGContextSetLineDash(context, self.movePoint == 0 ? 0 : -self.movePoint, lengths, 2);
}
CGContextDrawPath(context, kCGPathFillStroke);
}
- (void)setLineType:(BOOL)lineType
{
_lineType = lineType;
[self setNeedsDisplay];
}
- (void)setLineWidth:(CGFloat)lineWidth
{
_lineWidth = lineWidth;
[self setNeedsDisplay];
}
- (void)setLineColor:(UIColor *)lineColor
{
_lineColor = lineColor;
[self setNeedsDisplay];
}
- (void)setMovePoint:(CGFloat)movePoint
{
_movePoint = movePoint;
[self setNeedsDisplay];
}
- (void)setDrawPoint:(CGFloat)drawPoint
{
_drawPoint = drawPoint;
[self setNeedsDisplay];
}
- (void)setStepPoint:(CGFloat)stepPoint
{
_stepPoint = stepPoint;
[self setNeedsDisplay];
}
@end
Run