2011-03-10 80 views
1

什么,我试图做的是创建一个自定义进度条,石英图纸,我要做的就是以下,石英绘图古怪

- (void)drawRect:(CGRect)rect { 
    // Drawing code. 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGRect currentBounds = self.bounds; 
    CGContextSetLineWidth(context, 20.0f); 
    CGContextSetStrokeColorWithColor(context, [ProgressBarView redColor]); 

    CGContextBeginPath(context); 
    CGContextMoveToPoint(context, CGRectGetMinX(currentBounds), CGRectGetMidY(currentBounds)); 
    CGContextAddLineToPoint(context, CGRectGetMaxX(currentBounds) * time, CGRectGetMidY(currentBounds)); 
    CGContextClosePath(context); 
    CGContextDrawPath(context, kCGPathStroke); 
} 

在CGContextAddLineToPoint我乘maxX的时间的时间,这是每秒钟用这个函数计算。

- (void)pushTime { 
    if (time >= 1.0) { 
     time = 0.0; 
    } else { 
     time = time += 0.1; 
    } 
    [self setNeedsDisplay]; 
} 

它的伟大工程的第一次,但随后的进度条永远不会回到起点,我已经尝试启动值更改为0以外,使石英能够正常创建路径,但那没有做到。

无论如何,感谢您的帮助。

+0

什么“时间”是? INT?浮球?一个NSNumber ...?你在哪里调用pushTime?通过NSTimer? – meronix 2011-03-10 07:20:19

回答

1

看来你的观点并没有出于某种原因清除以前绘制的内容 - 您可以修复,明确强制视图重绘本身之前清除:

- (void)drawRect:(CGRect)rect { 
    // Drawing code. 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextClearRect(context, rect); 
    ... 

虽然我预计结算应该自动完成(和调整clearsContextBeforeDrawing属性的值不会产生任何效果......)