#import "MyLineView.h"
@implementation MyLineView
// 在UIView 上画虚线 创建文件MyLineView 继承与UIView
- (void)drawRect:(CGRect)rect
{
// 1.获取上下文环境
CGContextRef context = UIGraphicsGetCurrentContext();
// 2.设置线的颜色和宽度
CGContextSetStrokeColorWithColor(context,[UIColor redColor].CGColor);
CGContextSetLineWidth(context,1.0);
// 3.开始画线
CGContextBeginPath(context);
CGContextSetLineCap(context, kCGLineCapRound);
const CGFloat lengths[] = {10,5};
CGContextSetLineDash(context,0,lengths,2);
int baseLineHeight = 30.0f;
int numOfLines = (self.frame.size.height)/ baseLineHeight;
NSLog(@"线条的数:%d",numOfLines);
for (int i =0;i<numOfLines; i++) {
CGContextMoveToPoint(context,0.0,30.0*i);
CGContextAddLineToPoint(context, self.frame.size.width,30.0*i);
// 线的起点(0,30) 终点(self.frame.size.width,30)
// 线的宽度30
}
// 结束
CGContextClosePath(context);
CGContextStrokePath(context);
}