问题描述:

I have drawn a line using drawRect: in a file called 'LineView.m'.

In another file called 'ViewController.m' I am creating a 'else if' statement. When this statement is called it will change the color of the drawRect:. But how should I write something that does that?

I found an example for other objects like views, backgrounds, etc.

 self.box.backgroundColor = [UIColor clearColor];

But this will not work on the drawRect because I need to put something where 'box' is, that will identify the drawRect. But what?

Can I name my drawRect: and use the name to identify it?

网友答案:

Create a UIColor property in LineView.h called like lineColor. In LineView.m override the default setter of lineColor like so:

-(void)setLineColor:(UIColor *)lineColor
{
    _lineColor = lineColor;
    [self setNeedsDisplay];
}

Then in drawRect before you draw the line say:

[self.lineColor setStroke];

Now you can set the line color like so:

self.lineView.lineColor = [UIColor redColor/* or whatever you want*/];

EDIT: I misread your question the above will change the color of the line. To change the background, say something like self.lineView.backgroundColor = [UIColor someColor]; where lineView is whatever you're calling the instance of the LineView object.

相关阅读:
Top