Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 核心图形仅绘制黑色_Iphone_Objective C_Ios_Core Graphics - Fatal编程技术网

Iphone 核心图形仅绘制黑色

Iphone 核心图形仅绘制黑色,iphone,objective-c,ios,core-graphics,Iphone,Objective C,Ios,Core Graphics,我正试图在iPhone上开发一款国际象棋游戏,我被困在一个无关紧要的细节上,但似乎无法摆脱它。有人能看到丢失的那块吗 问题出在这里。当用户点击一个正方形时,它应该以某种褪色的颜色高亮显示。但实际发生的是,它只是画黑色,完全不考虑我设置它的颜色 这是我得到的图像。黑色圆圈应为红色或任何颜色。 这里,请注意我要绘制的UIView位于pieces视图后面。我怀疑这很重要,但我想完整 最后,该层的代码: - (id)initWithFrame:(CGRect)frame { self = [s

我正试图在iPhone上开发一款国际象棋游戏,我被困在一个无关紧要的细节上,但似乎无法摆脱它。有人能看到丢失的那块吗

问题出在这里。当用户点击一个正方形时,它应该以某种褪色的颜色高亮显示。但实际发生的是,它只是画黑色,完全不考虑我设置它的颜色

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

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

最后,该层的代码:

- (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等等,但是除了纯黑,我什么都得不到

编辑:

此外,以下是此视图的superview的代码:

@实施棋盘

-(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];

也许你应该这样说你的颜色: CGContextSetRGBStrokeColorcontext,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以便可以使用。

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

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

你说你在画一个图层。。。但是CALayer没有drawRect:方法,它有drawInContext:


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

您是否尝试过使用CGContextSetRGBFillColorNo,但仅此而已。哦,我知道会是这样的。颜色值的范围是0.0到1.0,而不是0.0到255.0。这是我的想法,但在弄清楚什么是错的混乱中,我把它保留在255。