gpt4 book ai didi

objective-c - 给矩形添加颜色

转载 作者:太空宇宙 更新时间:2023-11-04 07:29:34 27 4
gpt4 key购买 nike

我想用填充颜色制作一个圆圈。这是我的代码:

    context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetRGBStrokeColor(context, 0, 34, 102, 1);
CGContextSetRGBFillColor(context, 135, 206, 250, 0.5);
rectangle = CGRectMake(1, 1, 500, 500);
CGContextAddArc(context, pointWhereUserClickedX, pointWhereUserClickedY, 50, 0, 2*3.14159265359, YES);
CGContextDrawPath(context, kCGPathFillStroke);

当我运行它时,填充颜色是白色,即使我填充的是蓝色。当我想在两个“塔”矩形后面添加一个背景矩形时,我遇到了同样的问题:

context = UIGraphicsGetCurrentContext();

//Background styling
CGContextSetRGBFillColor(context, 202, 255, 112, 1);

//Background setup
background = CGRectMake(1, 1, 1024, 786);
CGContextAddRect(context, background);
CGContextDrawPath(context, kCGPathFill);

//Styling
CGContextSetLineWidth(context, 2.0);
CGContextSetRGBStrokeColor(context, 0, 0, 225, 1);
CGContextSetRGBFillColor(context, 0, 0, 225, 1);

//first tower setup
firstTower = CGRectMake(20, 20, 25, 100);
CGContextAddRect(context, firstTower);

//second tower setup
secondTower = CGRectMake(20, 800, 25, 100);
CGContextAddRect(context, secondTower);

//Draw towers
CGContextDrawPath(context, kCGPathFillStroke);

当我添加背景颜色时,我仍然看不到任何变化。它只是白色的,所以我猜它和圆圈是一样的问题。第二座塔也根本没有显示。

怎么了?或者我错过了什么?

最佳答案

Quartz 命令要求颜色参数在 0 和 1(浮点)范围内。此处的这一行(其他类似的行):

CGContextSetRGBFillColor(context, 135, 206, 250, 0.5);

实际上应该是:

CGContextSetRGBFillColor(context, 135.0/255.0, 206.0/255.0, 250.0/255.0, 0.5);

关于objective-c - 给矩形添加颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15124764/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com