UIBezierPath 和 applytransform

发布于 2025-01-06 23:42:00 字数 1618 浏览 1 评论 0原文

我正在尝试实现一个自定义 UIView,它基本上是一个饼图菜单(类似于分成切片的蛋糕)。

为此,我尝试从中心绘制一个圆和一系列线条,就像图表轮中的光线一样。

我已经成功地绘制了圆,现在我想绘制将圆分割成切片的线。

这就是我到目前为止所得到的:

-(void)drawRect:(CGRect)rect{

     [[UIColor blackColor] setStroke];
     CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGFloat minDim = (rect.size.width < rect.size.height) ? rect.size.width : rect.size.height;

    CGRect circleRect =  CGRectMake(0, rect.size.height/2-minDim/2, minDim, minDim); 

    CGContextAddEllipseInRect(ctx, circleRect);
    CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor yellowColor] CGColor]));
    CGContextFillPath(ctx);

    CGPoint start = CGPointMake(0, rect.size.height/2);
    CGPoint end = CGPointMake(rect.size.width, rect.size.height/2);

    for (int i = 0; i < MaxSlices(6); i++){

        CGFloat degrees = 1.0*i*(180/MaxSlices(6));
        CGAffineTransform rot = CGAffineTransformMakeRotation(degreesToRadians(degrees));

        UIBezierPath *path = [self pathFrom:start to:end];
        [path applyTransform:rot];

    }
 }   

- (UIBezierPath *) pathFrom:(CGPoint) start to:(CGPoint) end{

    UIBezierPath*    aPath = [UIBezierPath bezierPath];
    aPath.lineWidth = 5;
    [aPath moveToPoint:start];
    [aPath addLineToPoint:end];
    [aPath closePath];
    [aPath stroke];
    return aPath;
}

问题是路径上的 applyTransform 似乎没有做任何事情。第一条路径绘制正确,后续路径不受旋转影响。基本上我看到的只是一条路。在此处查看屏幕截图 http://img837.imageshack.us/img837/9757/iossimulatorscreenshotf。 png

谢谢您的帮助!

I am trying to implement a custom UIView which is basically a pie menu (something like a cake divided into slices).

For that I am trying to draw a circle and a series of lines from the center, like the rays in a chart's wheel.

I have successfully drawn the circle and and I would now like to draw the lines dividing the circle in slices.

This is what I have so far:

-(void)drawRect:(CGRect)rect{

     [[UIColor blackColor] setStroke];
     CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGFloat minDim = (rect.size.width < rect.size.height) ? rect.size.width : rect.size.height;

    CGRect circleRect =  CGRectMake(0, rect.size.height/2-minDim/2, minDim, minDim); 

    CGContextAddEllipseInRect(ctx, circleRect);
    CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor yellowColor] CGColor]));
    CGContextFillPath(ctx);

    CGPoint start = CGPointMake(0, rect.size.height/2);
    CGPoint end = CGPointMake(rect.size.width, rect.size.height/2);

    for (int i = 0; i < MaxSlices(6); i++){

        CGFloat degrees = 1.0*i*(180/MaxSlices(6));
        CGAffineTransform rot = CGAffineTransformMakeRotation(degreesToRadians(degrees));

        UIBezierPath *path = [self pathFrom:start to:end];
        [path applyTransform:rot];

    }
 }   

- (UIBezierPath *) pathFrom:(CGPoint) start to:(CGPoint) end{

    UIBezierPath*    aPath = [UIBezierPath bezierPath];
    aPath.lineWidth = 5;
    [aPath moveToPoint:start];
    [aPath addLineToPoint:end];
    [aPath closePath];
    [aPath stroke];
    return aPath;
}

The problem is that applyTransform on the path doesn't seem to do anything. The first path gest drawn correctly the and the following ones are unaffected by the rotation. Basically what I see is just one path. Check the screen shot here http://img837.imageshack.us/img837/9757/iossimulatorscreenshotf.png

Thank you for your help!

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

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

发布评论

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

评论(1

坏尐絯℡ 2025-01-13 23:42:00

您在转换路径之前先绘制路径(使用描边)。路径只是一种数学表示。这不是“屏幕上”的那条线。您无法通过修改有关数据来移动已经绘制的内容。

只需将 [aPath Stroke]pathFrom:to: 中移出,并将其放在 applyTransform: 后面即可。

You're drawing the path (with stroke) before you transform it. A path is just a mathematical representation. It isn't the line "on the screen." You can't move what you've already drawn by modifying the data about it.

Just move the [aPath stroke] out of pathFrom:to: and put it after the applyTransform:.

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