【发布时间】: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.0到1.0,而不是0.0到255.0 -
我是这么想的,但在弄清问题所在的混乱中,我把它留在了 255。
标签: iphone objective-c ios core-graphics