2012-02-15 36 views
0

私はiPhoneでチェスゲームを開発しようとしていますが、それ以外の重要な細部にこだわってしまってしまいました。誰かが欠けている作品を見ることができますか?コアグラフィックスが黒く塗りつぶす

ここに問題があります。ユーザーが四角形をクリックすると、色が薄くなります。実際に何が起こっているのですか、それは私がそれを設定した色に関係なく、黒を描くだけです。

ここに私が得るもののイメージがあります。黒丸は赤色または任意の色でなければなりません。 That black circle should be red!

ここでは、私が描いているUIViewが部分的に表示されていることに注意してください。疑問に思うのですが、私は完璧になりたいです。

Notice the black circle here is behind the pawn

最後に、層のためのコード:

- (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の値を使用して、多くのことを試してみましたが、私はしています固体黒以外の何かを得ることはできません。

EDIT:

また、ここでは、このビューのスーパーのコードです:

@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]; 
+3

'CGContextSetRGBFillColor'を使ってみましたか? –

+0

いいえ、それだけです。ああ、私はそれがそれのようなものであることを知っていた。 – jakev

+1

色の値の範囲は「0.0」〜「1.0」で、「0.0」〜「255.0」ではありません。 – JustSid

答えて

0

たぶん、あなたはあなたの色をこのように明記してください:
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を追加するまで、これはうまくいく可能性があります。

0

CGContextSetRGBStrokeColorの色成分値(赤、緑、青、アルファ)は0と1の間である必要があります。255が色の最大値であると仮定しているので、すべての色成分値255.0f。この方法は、それがdrawInContextがあります:

私はのdrawRectを呼び出すために試みることによって推測:あなたの中にあなたはレイヤーに描画していると言う...しかし、CALayerのはのdrawRectを持っていない

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

真のコンテキストが正しく構築されていないCALayerのインスタンス、またはそれを持っていますが、あなたはそれに縛られていません。

関連する問題