i need to draw two lines. can i use the same UIView subclass to make both draws? after i create the UIView
draw2D *myView = [[draw2D alloc] initWithFrame:myRect];
if i change the method to use variables, can i change those values and recall the drawRect method to draw a different line?
- (void)drawRect:(CGRect)rect
{
CGContextRef context01 = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context01, 1.0);
CGContextSetStrokeColorWithColor(context01, [[UIColor blackColor]CGColor]);
CGContextMoveToPoint(context01, 0, 0);
CGContextAddLineToPoint(context01, 800, 0);
CGContextStrokePath(context01);
CGContextRef context02 = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context02, 1.0);
CGContextSetStrokeColorWithColor(context02, [[UIColor blackColor]CGColor]);
CGContextMoveToPoint(context02, 453, 0);
CGContextAddLineToPoint(context02, 453, 800);
CGContextStrokePath(context02);
}
Just call setNeedsDisplay on a view to force its drawRect method to be called again.
It doesn’t redraw the view immediately, but it flags it as needing to be drawn again in the next view update cycle (updates happen roughly every 60th second). That means you can call setNeedsDisplay multiple times with no performance penalty.