创建自定义UI后调用了drawRect吗? [英] drawRect called after creating a custom UI?

查看:27
本文介绍了创建自定义UI后调用了drawRect吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要画两条线.我可以使用相同的UIView子类进行两次绘制吗?在创建UIView之后

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];

如果我将方法更改为使用变量,是否可以更改这些值并重新调用drawRect方法以绘制另一条线?

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

推荐答案

只需在视图上调用setNeedsDisplay即可强制再次调用其drawRect方法.

Just call setNeedsDisplay on a view to force its drawRect method to be called again.

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

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.

这篇关于创建自定义UI后调用了drawRect吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆