Reputation: 789
I'm trying to use a loop to build a variable number of CGMutablePathRef's in order to draw a variable number of filled in wedges on a circle. For some reason, though, at the end of my loop my array returns with only one object. Here is the method I'm using to construct the array:
+ (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;
}
Edit New attempt using UIBezierPath's:
+ (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;
}
The code that calls this function:
- (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);
}
}
Upvotes: 3
Views: 1636
Reputation: 789
The answer to this was much simpler than memory leaks, although the memory leaks would undoubtedly have led to problems down the road. My code simply wouldn't run because I accidentally placed a semicolon at the end of a for
line, so the for
loop didn't even run.
Upvotes: 1