Ios 为什么这里的颜色不对?
下面的代码生成一个纯红色正方形:Ios 为什么这里的颜色不对?,ios,colors,core-graphics,uicolor,Ios,Colors,Core Graphics,Uicolor,下面的代码生成一个纯红色正方形: - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); CGContextSetLineWidth(context, 5.0f); CGContextStrokeRect(co
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 5.0f);
CGContextStrokeRect(context, rect);
}
但是下面的代码会导致一个白色的正方形,而我期望的是一个不同的红色阴影。谁能告诉我这是为什么
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor* boxColour = [UIColor colorWithRed:217 green:37 blue:70 alpha:1.0];
CGContextSetStrokeColorWithColor(context, boxColour.CGColor);
CGContextSetLineWidth(context, 5.0f);
CGContextStrokeRect(context, rect);
}
UIColor
类的colorWithRed:green:blue:alpha:
方法采用0.0到1.0范围内的参数。您可能只需要将所有值(alpha除外)除以255.0f
:
UIColor* boxColour = [UIColor colorWithRed:217/255.0f green:37/255.0f blue:70/255.0f alpha:1.0f];