【问题标题】:Custom view sometimes appears with wrong background color自定义视图有时会出现错误的背景颜色
【发布时间】:2014-12-31 21:58:49
【问题描述】:

我有一个自定义的“点”视图,它是一个带有边框和背景颜色的圆圈,类似于 iOS 7+ 左上角显示信号强度的点。

点可以填充也可以不填充。

填充点:

未填充的点:

问题:在我启动应用程序的 20-30% 的时间里,未填充的点会以黄色背景显示,即使明确指定了白色。

这里是drawRect中的代码:

- (void)drawRect:(CGRect)rect
{
    CGRect borderRect = CGRectInset(rect, 3, 3);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    if (self.filled) {
        CGContextSetFillColor(ctx, CGColorGetComponents([self.color CGColor]));
    } else {
        CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor whiteColor] CGColor]));
    }
    CGContextSetStrokeColor(ctx, CGColorGetComponents([self.color CGColor]));
    CGFloat lineWidth = rect.size.height/10;
    if (lineWidth < 1) {
        lineWidth = 1.0;
    }
    CGContextSetLineWidth(ctx, lineWidth);
    CGContextFillEllipseInRect(ctx, borderRect);
    CGContextStrokeEllipseInRect(ctx, borderRect);
    CGContextFillPath(ctx);
}

self.color 永远不会被设置为黄色。只有您看到的蓝色作为边框。

什么可能导致这些有时显示为黄色?

【问题讨论】:

    标签: ios iphone core-graphics


    【解决方案1】:

    我认为这可能与这篇文章中提到的问题有关:Is there an issue with CGColorGetComponents?。当我替换这一行时,

     CGContextSetFillColor(ctx, (CGFloat []){ 1, 1, 1, 1 });
    

    对于你使用的那个,

    CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor whiteColor] CGColor]));
    

    我没有看到任何偶尔出现的黄色(我主要看到黑色,运行您的代码时偶尔会看到黄色或白色)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2014-11-17
      • 2011-08-23
      • 2015-09-21
      • 1970-01-01
      • 2012-07-10
      相关资源
      最近更新 更多