iOS Core Graphics封装虚线
2016-07-18 23:02
399 查看
HJDashLineView.h
HJDashLineView.m
Run
// // 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
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法