如何绘制一个充满四种颜色的圆圈?

时间:2010-06-24 18:47:17

标签: iphone

任何人?我想用核心图形绘制一个圆圈,里面有四种颜色。即四个均匀分布的颜色,每个四分之一的圆圈。

1 个答案:

答案 0 :(得分:5)

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect mainSquare = self.bounds;
    CGContextSaveGState(context);

    // Create circle path and clip

    CGContextAddEllipseInRect(context, mainSquare);
    CGContextClip(context);

    // Define rects

    CGRect topLeft = CGRectMake(mainSquare.origin.x, mainSquare.origin.y, mainSquare.size.width / 2, mainSquare.size.height / 2);
    CGRect topRight = CGRectMake(mainSquare.size.width / 2, mainSquare.origin.y, mainSquare.size.width / 2, mainSquare.size.height / 2);
    CGRect bottomLeft = CGRectMake(mainSquare.origin.x, mainSquare.size.height / 2, mainSquare.size.width / 2, mainSquare.size.height / 2);
    CGRect bottomRight = CGRectMake(mainSquare.size.width / 2, mainSquare.size.height / 2, mainSquare.size.width / 2, mainSquare.size.height / 2);

    // Define colors
    CGColorRef topLeftColor = [[UIColor redColor] CGColor];
    CGColorRef topRightColor = [[UIColor blueColor] CGColor];
    CGColorRef bottomLeftColor = [[UIColor greenColor] CGColor];
    CGColorRef bottomRightColor = [[UIColor yellowColor] CGColor];

    // Fill rects with colors
    CGContextSetFillColorWithColor(context, topLeftColor);
    CGContextFillRect(context, topLeft);

    CGContextSetFillColorWithColor(context, topRightColor);
    CGContextFillRect(context, topRight);

    CGContextSetFillColorWithColor(context, bottomLeftColor);
    CGContextFillRect(context, bottomLeft);

    CGContextSetFillColorWithColor(context, bottomRightColor);
    CGContextFillRect(context, bottomRight);


    CGContextRestoreGState(context);

}