2013-02-27 45 views
-1

我想用颜色填充矩形。但我的代码不起作用(没有矩形出现)。请告诉我这段代码有什么问题?无法为CGRect设置填充颜色/路径石英

- (void)drawRect:(CGRect)rect 
{ 
context = UIGraphicsGetCurrentContext(); 
CGContextSetRGBFillColor(context, 84, 84, 84, 1); 
firstTower = CGRectMake(20, 20, 50, 200); 
CGContextDrawPath(context, kCGPathFillStroke); 

if (_touchHasBegun) 
{ 
    context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 2.0); 
    CGContextSetRGBStrokeColor(context, 0, 34, 102, 1);      
    CGContextSetRGBFillColor(context, 135, 206, 250, 0.25); 
    rectangle = CGRectMake(1, 1, 500, 500); 
    CGContextAddArc(context, pointWhereUserClickedX, pointWhereUserClickedY, 50, 0, 2*3.14159265359, YES); 
    CGContextDrawPath(context, kCGPathFillStroke); 


} 

}

如果语句是一个圆。

+0

“不起作用”是什么意思? – 2013-02-27 21:36:57

+0

我实际上通过删除第一个CGCOntextDrawPath行来解决这个问题。但请看一个新问题的更新问题。 – JimmyYXA 2013-02-27 21:57:54

回答

0

矩形不会出现,因为您没有将其添加到上下文中。您已创建CGRect并将其分配给firstTower变量,但您永远不会告诉它的上下文。

你可能想分配给firstTower后添加

CGContextAddRect(context, firstTower); 

同样,你没有在第二次绘制中添加rectangle值。