5

我正在尝试实现一个自定义 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

感谢您的帮助!

4

1 回答 1

4

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

只需将其[aPath stroke]移出pathFrom:to:并放在applyTransform:.

于 2012-02-16T15:18:35.780 回答