0

I am drawing in a uiview by subclassing a uiview and drawing code is in the

- (void)drawRect:(CGRect)rect

method. I am repeatedly calling this to get a growing effect for a line. But every time the method calls the view get cleared. Only small line ig ploted in the view my complete code is

- (void)drawRect:(CGRect)rect
{
    static int i=0;
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.5);       
    CGContextSetLineWidth(context, 10.0);        
    CGContextMoveToPoint(context, i, i);
    CGContextAddLineToPoint(context, i+10, i+10);  
    NSLog(@"-----%d",i);    
    CGContextClosePath(context);            
    CGContextDrawPath(context, kCGPathFillStroke); 
    i++;
    if(i<100)
    [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.033];
}

How can I make a growing effect for line

4

2 回答 2

1

在这种情况下,您将重绘所有视图,因此您需要使用CGContextMoveToPoint(context, 0, 0);. 所以每次你执行所有的绘图。您也可以调用setNeedsDisplayInRect:仅重绘一小部分,但在这种情况下,您应该检查在重绘所有视图时执行所有绘图的矩形。

于 2012-04-19T07:40:55.253 回答
0

每次调用DrawRect 时都会绘制您的所有UI,而不是现有部分的附加部分 - 所以实际上目前您每次都绘制一条线。您必须在每次迭代中绘制一个大小不断增长的矩形,以达到您想要的效果。

于 2012-04-19T08:45:58.227 回答