任何人?我想用核心图形绘制一个圆圈,里面有四种颜色。即四个均匀分布的颜色,每个四分之一的圆圈。
答案 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);
}