开发者

iPhone App - Why do my CGContext circles look like black squares?

开发者 https://www.devze.com 2023-03-17 06:45 出处:网络
What is wrong with this drawRect method in my iPhone app custom UIView class? It\'s supposed to draw a colored circle, however it draws a black square of proper width and height.

What is wrong with this drawRect method in my iPhone app custom UIView class? It's supposed to draw a colored circle, however it draws a black square of proper width and height.

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    UIColor *c = [UIColor redColo开发者_运维百科r];
    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));
}


Try this:

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


Hope this works fine.

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

精彩评论

暂无评论...
验证码 换一张
取 消