【发布时间】:2015-08-16 16:33:44
【问题描述】:
我对以下代码的工作方式感到困惑。 预计会产生一个被彩色圆圈包围的黑色圆盘。 它适用于某些颜色(如 cmets 中所述),但不适用于其他颜色。 谁能解释这种神秘的行为?
- (void)drawRect:(CGRect)rect
{
CGRect rectangle; CGFloat shiftVal=2.0,lineWidth=3.0;
rectangle.origin=CGPointMake(shiftVal, shiftVal);
rectangle.size=self.frame.size;
rectangle.size.width-=shiftVal*2;
rectangle.size.height-=shiftVal*2;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextFillEllipseInRect(context, rectangle);
CGContextSetLineWidth(context, lineWidth);
const CGFloat *components = CGColorGetComponents([UIColor greenColor].CGColor); // Works as expected.
//const CGFloat *components = CGColorGetComponents([UIColor darkGrayColor].CGColor); // No surrounding circle (instead of darkGray).
//const CGFloat *components = CGColorGetComponents([UIColor lightGrayColor].CGColor); // Produces a greenish circle (instead of lightGray)
// Draw the outer circle:
CGContextSetRGBStrokeColor(context,
components[0],
components[1],
components[2],
components[3]);
CGContextStrokeEllipseInRect(context, rectangle);
}
【问题讨论】:
-
在深入研究这个问题之前 - 您知道如果某些东西不像预期的那样看起来是个好主意吗?图片!!!两到三张截图就完美了。
-
将对
CGContextSetRGBStrokeColor的调用替换为一行:[[UIColor someColor] setStroke];。
标签: ios uiview drawrect cgcontextref