2011-07-05 39 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)); 
} 
相關問題