iPhone App - Почему мои круги CGContext выглядят как черные квадраты? - PullRequest
1 голос
/ 06 июля 2011

Что не так с этим методом drawRect в моем пользовательском классе приложения UIView для iPhone?Предполагается нарисовать цветной круг, однако он рисует черный квадрат правильной ширины и высоты.

- (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));
}

Ответы [ 2 ]

0 голосов
/ 28 июля 2012

Надеюсь, это хорошо работает.

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context , [UIColor redColor].CGColor;
    CGContextFillEllipseInRect(context, CGRectMake(x, y, width, width));
}
0 голосов
/ 06 июля 2011

Попробуйте это:

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