iOS Core Graphics封装虚线

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值