【问题标题】:Core Graphics drawing only black核心图形仅绘制黑色
【发布时间】:2012-02-15 14:59:58
【问题描述】:

我正在尝试在 iPhone 上开发一个国际象棋游戏,并且卡在一个原本微不足道的细节上,但似乎无法超越它。任何人都可以看到丢失的部分吗?

这就是问题所在。当用户点击一个正方形时,它应该以某种褪色的颜色突出显示。但实际上发生了什么,它只是绘制黑色,完全不管我将它设置为什么颜色。

这是我得到的图像。黑色圆圈应该是红色的,或者任何颜色。

在这里,请注意我正在绘制的 UIView 位于片段视图的后面。怀疑这很重要,但我想要完整。

最后是图层的代码:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        rowSelected = 0;
        colSelected = 0;
    }
    return self;
}

-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);
    CGContextSetRGBStrokeColor(context, 255.0, 0.0, 0.0, 1.0);

    if (rowSelected && colSelected)
    {
        int gridsize = (self.frame.size.width / 8);
        int sx = (colSelected-1) * gridsize;
        int sy = (rowSelected-1) * gridsize;
        int ex = gridsize;
        int ey = gridsize;

        CGContextFillEllipseInRect(context, CGRectMake(sx,sy,ex,ey));
    }
}
// Handles the start of a touch
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    int gridsize = (self.frame.size.width / 8);
    // Position of touch in view
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint loc = [touch locationInView:self.superview];
    int x = loc.x / gridsize;
    int y = loc.y / gridsize;

    rowSelected = y + 1;
    colSelected = x + 1;

    [self setNeedsDisplay];
}

我尝试了很多方法,使用值 1 而不是 255,使用 alpha 等,但除了纯黑色之外我什么也得不到。

编辑:

另外,这里是这个视图的父视图的代码:

@implementation 棋盘

-(id)initWithFrame:(CGRect)frame
{
    if (self != [super initWithFrame:frame])
        return self;

    int SQUARE_SIZE = frame.size.width / 8;

    currentSet = BW;

    UIImage *img = [UIImage imageNamed:@"board.png"];
    background = [[UIImageView alloc] initWithImage:img];
    [background setFrame:frame];
    [self addSubview:background];


    overlay = [[ChessBoardOverlay alloc] initWithFrame:frame];
    [self addSubview:overlay];

【问题讨论】:

  • 你试过用CGContextSetRGBFillColor
  • 不,但就是这样。哦,我就知道会是这样。
  • 颜色值的范围是0.01.0,而不是0.0255.0
  • 我是这么想的,但在弄清问题所在的混乱中,我把它留在了 255。

标签: iphone objective-c ios core-graphics


【解决方案1】:

在函数CGContextSetRGBStrokeColor 中,颜色分量值(红色、绿色、蓝色、alpha)需要介于 0 和 1 之间。由于您假设 255 是颜色的最大值,因此您需要除以所有颜色分量255.0f 的值。

CGContextSetRGBStrokeColor(context, 255.0f/255.0f, 0.0/255.0f, 0.0/255.0f, 1.0);

【讨论】:

    【解决方案2】:

    你说你正在绘制一个图层...但是 CALayer 没有 drawRect: 方法,它有 drawInContext:

    我猜想通过尝试调用 drawRect: 在您的 CALayer 实例中没有正确构建真正的上下文,或者它已经正确构建但您没有绑定它。

    【讨论】:

      【解决方案3】:

      也许你应该这样表述你的颜色:
      CGContextSetRGBStrokeColor(context, 255.0/255.0f, 0.0/255.0f, 0.0/255.0f, 1.0f);

      这发生在我曾经使用过:

      [UIColor colorWithRed:12.0/255.0f green:78.0/255.0f blue:149.0/255.0f alpha:1.0f];
      

      直到我添加了/255.0f 这样它才能工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        • 1970-01-01
        • 2013-10-17
        • 2016-02-27
        • 1970-01-01
        相关资源
        最近更新 更多