tmp*_*mpz 5 quartz-graphics quartz-2d ios uibezierpath
我正在尝试实现一个自定义 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;
}
Run Code Online (Sandbox Code Playgroud)
问题是路径上的 applyTransform 似乎没有做任何事情。正确绘制的第一条路径和以下路径不受旋转影响。基本上我看到的只是一条路。检查这里的屏幕截图http://img837.imageshack.us/img837/9757/iossimulatorscreenshotf.png
感谢您的帮助!
stroke您在变换路径之前先绘制路径(使用)。路径只是一种数学表示。这不是“屏幕上”的那条线。您无法通过修改有关数据来移动已经绘制的内容。
只需将 移出[aPath stroke]并pathFrom:to:放在 后面applyTransform:即可。
| 归档时间: |
|
| 查看次数: |
6255 次 |
| 最近记录: |