我正試圖在iPhone上開發一款國際象棋遊戲,並且陷入了其他微不足道的細節,但似乎無法通過它。任何人都可以看到失蹤的作品嗎?Core Graphics drawing only only
這是問題所在。當用戶點擊一個正方形時,它應該以某種褪色的顏色突出顯示。究竟發生了什麼,它只是畫黑,完全不管我設定的顏色。
下面是我得到的圖像。黑色圓圈應該是紅色或任何顏色。
在這裏,請注意我繪製的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的值,與α演奏等,但我除了堅實的黑色以外不能獲得其他任何東西。
編輯:
而且,這裏是這種觀點的上海華盈的代碼:
@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];
您是否嘗試過使用'CGContextSetRGBFillColor' – 2012-02-15 15:07:16
不,但就是這樣。哦,我知道它會是這樣的。 – jakev 2012-02-15 15:08:43
顏色值的範圍是從'0.0'到'1.0',而不是從'0.0'到'255.0' – JustSid 2012-02-15 15:10:02