为 iPhone 创建 CGMutablePathRef 数组?

发布于 2024-12-04 09:07:22 字数 2402 浏览 1 评论 0原文

我正在尝试使用循环来构建可变数量的 CGMutablePathRef,以便在圆上绘制可变数量的填充楔形。但出于某种原因,在循环结束时,我的数组仅返回一个对象。这是我用来构造数组的方法:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最终幸福 2024-12-11 09:07:22

这个问题的答案比内存泄漏简单得多,尽管内存泄漏无疑会导致以后出现问题。我的代码根本无法运行,因为我不小心在 for 行末尾放置了一个分号,因此 for 循环甚至没有运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文