ループを使用して可変数の CGMutablePathRef を構築し、可変数の塗りつぶされたウェッジを円に描画しようとしています。ただし、何らかの理由で、ループの最後に、配列は 1 つのオブジェクトのみを返します。配列を構築するために使用している方法は次のとおりです。
+ (NSMutableArray *)pathForCircleWithRect:(CGRect)rect numOfWedges:(NSUInteger)num
{
CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
CGFloat radius = rect.size.width / 2;
CGFloat angle = RADIANS(360) / num;
CGFloat startAngle = 0;
CGFloat endAngle = startAngle + angle;
NSMutableArray *paths = [NSMutableArray array];
for (int x = 0; x < num; x++);
{
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, center.x, center.y);
CGPathAddArc(path, NULL, center.x, center.y, radius, startAngle, endAngle, 0);
CGPathAddLineToPoint(path, NULL, center.x, center.y);
[paths addObject:(id)path];
startAngle = endAngle;
endAngle = startAngle + angle;
}
return paths;
}
UIBezierPath を使用して新しい試行を編集します。
+ (NSMutableArray *)pathForCircleWithRect:(CGRect)rect numOfWedges:(NSUInteger)num
{
CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
CGFloat radius = rect.size.width / 2;
CGFloat angle = RADIANS(360) / num;
CGFloat startAngle = 0;
CGFloat endAngle = startAngle + angle;
NSMutableArray *paths = [NSMutableArray array];
for (int x = 0; x < num; x++);
{
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, center.x, center.y);
CGPathAddArc(path, NULL, center.x, center.y, radius, startAngle, endAngle, 0);
CGPathAddLineToPoint(path, NULL, center.x, center.y);
[paths addObject:[UIBezierPath bezierPathWithCGPath:path]];
startAngle = endAngle;
endAngle = startAngle + angle;
}
return paths;
}
この関数を呼び出すコード:
- (void)drawRect:(CGRect)rect
{
int num = 18;
NSMutableArray *paths = [WheelView pathForCircleWithRect:rect numOfWedges:num];
NSMutableArray *colors = [WheelView colorsForCircleWithNumOfWedges:num];
CGContextRef context = UIGraphicsGetCurrentContext();
for (int x = 0; x < num; x++)
{
CGContextSetFillColorWithColor(context, (CGColorRef)[colors objectAtIndex:x]);
CGContextSaveGState(context);
CGMutablePathRef wedgePath = (CGMutablePathRef)[paths objectAtIndex:x];
CGContextAddPath(context, wedgePath);
CGContextDrawPath(context, kCGPathFill);
//CGContextClip(context);
CGContextRestoreGState(context);
}
}