2012-02-15 111 views
0

我正試圖在iPhone上開發一款國際象棋遊戲,並且陷入了其他微不足道的細節,但似乎無法通過它。任何人都可以看到失蹤的作品嗎?Core Graphics drawing only only

這是問題所在。當用戶點擊一個正方形時,它應該以某種褪色的顏色突出顯示。究竟發生了什麼,它只是畫黑,完全不管我設定的顏色。

下面是我得到的圖像。黑色圓圈應該是紅色或任何顏色。 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的值,與α演奏等,但我除了堅實的黑色以外不能獲得其他任何東西。

編輯:

而且,這裏是這種觀點的上海華盈的代碼:

@implementation器ChessBoard

-(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' – 2012-02-15 15:07:16

+0

不,但就是這樣。哦,我知道它會是這樣的。 – jakev 2012-02-15 15:08:43

+1

顏色值的範圍是從'0.0'到'1.0',而不是從'0.0'到'255.0' – JustSid 2012-02-15 15:10:02

回答

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中,顏色組件值(紅色,綠色,藍色,alpha)需要介於0和1之間。由於您假定255是某種顏色的最大值,因此需要將所有顏色分量值到255.0f。

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

你說你正在繪製一個層......但CALayer的沒有一個drawRect:方法,它有drawInContext:

我會試圖通過調用的drawRect猜測:在你的一個CALayer的實例沒有正確構建真正的上下文,或者它有,但你沒有綁定它。