【问题标题】:Quartz Drawing weirdly奇怪的石英画
【发布时间】:2011-03-10 05:54:27
【问题描述】:

我要做的是创建一个自定义进度条,用石英绘图,我做的是以下,

- (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 以外的值,以便石英可以毫无问题地创建路径,但这并没有去做吧。

无论如何,谢谢你的帮助。

【问题讨论】:

  • 什么是“时间”?一个INT?一个浮子?一个NSNumber ...?你在哪里叫pushTime?通过 NSTimer?

标签: iphone quartz-graphics drawrect


【解决方案1】:

您的视图似乎由于某种原因没有清除以前绘制的内容 - 您可以通过在重绘之前明确强制视图清除来解决此问题:

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

    CGContextRef context = UIGraphicsGetCurrentContext();  

    CGContextClearRect(context, rect);
    ...

虽然我希望清除应该自动完成(并且调整 clearsContextBeforeDrawing 属性的值不会产生任何效果......)

【讨论】:

    猜你喜欢
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多