2011-07-05 41 views
1

这个drawRect方法在我的iPhone应用程序自定义UIView类中出现了什么问题?它应该绘制一个彩色的圆圈,但它会绘制一个宽度和高度都适当的黑色方块。iPhone应用程序 - 为什么我的CGContext圈看起来像黑色方块?

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 2.0); 
    UIColor *c = [UIColor redColor]; 
    const CGFloat *components = CGColorGetComponents([c CGColor]); 
    CGFloat red = components[0]; 
    CGFloat green = components[1]; 
    CGFloat blue = components[2]; 
    CGFloat al = components[3];  
    CGContextSetRGBFillColor(context, red, green, blue, al); 
    CGContextFillEllipseInRect(context, CGRectMake(x, y, width, width)); 
} 

回答

0

试试这个:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextAddEllipseInRect(ctx, CGRectMake(x, y, width, width)); 
    CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor redColor] CGColor])); 
    CGContextFillPath(ctx); 
} 
0

希望这会工作得很好。

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 2.0); 
    CGContextSetFillColorWithColor(context , [UIColor redColor].CGColor; 
    CGContextFillEllipseInRect(context, CGRectMake(x, y, width, width)); 
} 
相关问题