objective-c - 给矩形添加颜色

标签 objective-c c ipad quartz-graphics

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

    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/

相关文章:

objective-c - 向 iOS 应用程序添加另一个 UIWindow 的优点、问题、示例?

ios - 使用 MBProgressHUD 时的内存泄漏

ios - 为什么 NSIndexPath indexPathForItem 返回无效路径?

c++ - 如何在 mfc 中为多线程应用程序创建公共(public)日志文件?

xcode - putpkt:写入失败,管道损坏

ios - 适用于 iOS 12.1 的 flutter : black screen instead of an app

iphone - 尝试使用触摸事件在另一个图像之上绘制图像

c - 我为 Jacobi 迭代方法编写了这个 C 程序,但我得到 Nan 和无穷大作为我尝试更改的输出

c - 是否可以从 Lua 函数写入在 C 中打开的文件描述符?

ios - 如何计算 iPad 上模态视图的底部偏移量?