在我的 ViewController 我有一个按钮:
- (IBAction)drawLineClick:(id)sender
{
CGRect rect;
rect.origin.x = 20.0f;
rect.origin.y = 40.0f;
rect.size.width = 100.0f;
rect.size.height = 100.0f;
//draw line
DrawLine *drawLine = [[DrawLine alloc] initWithFrame:rect];
[self.view addSubview:drawLine];
}
在我的 DrawLine 类中,我只画一条线:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[super setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
[self drawLine];
}
- (void)drawLine
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetLineWidth(context, 3.0);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 50, 50);
CGContextStrokePath(context);
}
这很好用,但这不是可变的。每次都是同一行。如何将 ViewController 中的线条颜色、线条宽度等传递给 DrawLine 类,以便绘制不同的线条?
谢谢。