Uicolor和图形上下文
-
14-11-2019 - |
题
我有一个自定义UIView,它绘制了如此的路径
- (void)drawRect:(CGRect)rect{
[[UIColor blackColor] setStroke];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColor(ctx, CGColorGetComponents(self.color));
CGContextFillPath(ctx);
UIBezierPath *thePath = [UIBezierPath bezierPath];
thePath.lineWidth = _lineWidth;
_center = CGPointMake(rect.size.width, rect.size.height);
[thePath moveToPoint:_center];
[thePath addArcWithCenter:_center radius:rect.size.width startAngle:M_PI endAngle:M_PI+degreesToRadians(_angle)clockwise:YES];
[thePath closePath];
[thePath fill];
}
.
我的自定义UIView还有一种改变填充颜色的方法
- (void) changeColor:(UIColor *) newColor {
self.color = [newColor CGColor];
[self setNeedsDisplay];
}
.
如果我调用changecolor:方法与任何预定义颜色,如
[UIColor redColor]
.
一切正常。相反,如果我试图给它一个自定义颜色,例如
UIColor* color = [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f];
.
我的自定义UIView闪烁在白色,红色和蓝色之间的颜色随机。
任何思想为什么?
提前谢谢!
解决方案
尝试:
CGContextSetFillColorWithColor(ctx, self.color.CGColor);
.
而不是:
CGContextSetFillColor(ctx, CGColorGetComponents(self.color));
. 其他提示
当您使用此方法时调用:
[UIColor redColor];
.
为了优化目的,Uicolor仅用2个组件返回一个对象,因为他不需要另一个组件。(这是类聚类机制可能的)。
也许是为什么您的代码在这两种颜色之间显示一些差异。
不隶属于 StackOverflow