import "LineView.h" 继承的是uiview 是一个单独的类
-
(void)drawRect:(CGRect)rect
{// Drawing code // 1.获得图形上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接图形(路径) // 设置线段宽度 CGContextSetLineWidth(ctx, 10); // 设置线段头尾部的样式 CGContextSetLineCap(ctx, kCGLineCapRound); // 设置线段转折点的样式 CGContextSetLineJoin(ctx, kCGLineJoinRound); /** 第1根线段 **/ // 设置颜色 CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1); // 设置一个起点 CGContextMoveToPoint(ctx, 10, 10); // 添加一条线段到(100, 100) CGContextAddLineToPoint(ctx, 100, 100); // 渲染一次 CGContextStrokePath(ctx); // 渲染显示到view上面 CGContextStrokePath(ctx);
}
以下是controller 调用 为什么没有显示
import "LineView.h"
-
(void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor = [UIColor redColor];
LineView *line = [[LineView alloc] init];
// [line setNeedsDisplay];
[self.view addSubview:line];
}
initWithFrame你应该给指定一个大小,init默认是CGRectZero