我们可以通过Core Graphics 和 UIKit框架画直线
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);//设置线的颜色
CGContextSetLineWidth(ctx, 2);//设置线的宽度
CGContextSetLineCap(ctx, kCGLineCapRound);//设置线的起始端的样式
CGContextSetLineJoin(ctx, kCGLineJoinRound);//设置线的连接样式
//画一条线
CGContextMoveToPoint(ctx, 10, 50);
CGContextAddLineToPoint(ctx, 100, 50);
//同时依次画多条线
CGPoint lines[] = {
CGPointMake(10.0, 90.0),
CGPointMake(70.0, 60.0),
CGPointMake(130.0, 90.0),
CGPointMake(190.0, 60.0),
CGPointMake(250.0, 90.0),
CGPointMake(310.0, 60.0),
};
//参数二:线数组,参数三:线的个数
CGContextAddLines(ctx, lines, sizeof(lines)/sizeof(lines[0]));
CGContextStrokePath(ctx);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 50)];
[path addLineToPoint:CGPointMake(100, 50)];
path.lineWidth = 2;
[[UIColor blackColor] setStroke];
[path stroke];
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10.0, 90.0)];
[path addLineToPoint:CGPointMake(70.0, 60.0)];
[path addLineToPoint:CGPointMake(130.0, 90.0)];
[path addLineToPoint:CGPointMake(190.0, 60.0)];
[path addLineToPoint:CGPointMake(250.0, 90.0)];
[path addLineToPoint:CGPointMake(310.0, 60.0)];
path.lineWidth = 2;
[path stroke];
还可以画虚线
CGContext中
//设置虚线
CGFloat length[] = {10,5,10};
CGContextSetLineDash(ctx, 8, length, 3);
UIBezierPath中
CGFloat length[] = {10,5,10};
[path setLineDash:length count:3 phase:8];