继承UIView 重写drawRect方法
#import "CustomLine.h"
@implementation CustomLine
-(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 2);//线宽
CGContextSetAllowsAntialiasing(context, true);
CGContextSetRGBStrokeColor(context, 210.0/ 255.0,210.0/ 255.0 , 210.0/ 255.0 , 1.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0, 10);//起点坐标
CGContextAddLineToPoint(context,0, 30);//坐标终点
CGContextStrokePath(context);
}
其他类中调用
CustomLine *line = [[CustomLine alloc]init];
line.backgroundColor = [UIColor whiteColor];
line.frame = CGRectMake(kScreenWidth/ 2 ,0, 10, view.frame.size.height);
[view addSubview:line];
@end