创build自定义用户界面后调用drawRect?

我需要画两条线。 我可以使用相同的UIView子类来使两个绘制? 之后我创buildUIView

draw2D *myView = [[draw2D alloc] initWithFrame:myRect]; 

如果我改变使用variables的方法,我可以改变这些值,并调用drawRect方法来绘制不同的线?

 - (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); } 

只需在视图上调用setNeedsDisplay来强制其drawRect方法再次被调用。

它不会立即重新绘制视图,但会将其标记为需要在下一个视图更新周期中再次绘制(更新大约每60秒发生一次)。 这意味着您可以多次调用setNeedsDisplay,而不会影响性能。

我可以使用相同的UIView子类来使两个绘制?

当然

如果我改变使用variables的方法,我可以改变这些值,并调用drawRect方法来绘制不同的线?

通常情况下,你只需要为这些variables创build一个新的函数或方法:

 static inline void imp_DrawLine(CGContextRef gtx, CGPoint start, CGPoint end) { CGContextMoveToPoint(gtx, start.x, start.y); CGContextAddLineToPoint(gtx, end.x, end.y); CGContextStrokePath(gtx); } - (void)drawRect:(CGRect)rect { CGContextRef gtx = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(gtx, 1.0); CGContextSetStrokeColorWithColor(gtx, [UIColor blackColor].CGColor); imp_DrawLine(gtx, CGPointMake(0, 0), CGPointMake(800, 0)); imp_DrawLine(gtx, CGPointMake(453, 0), CGPointMake(453, 800)); }