Ant*_*tor 4 core-graphics objective-c drawrect ios uibezierpath
我正在尝试使用UIBezierPath绘制一个PieChart,我很接近这样做,但是,我有一个问题,你可以在附带的截图上看到
这是我正在使用的代码:
-(void)drawRect:(CGRect)rect
{
CGRect bounds = self.bounds;
CGPoint center = CGPointMake((bounds.size.width/2.0), (bounds.size.height/2.0));
NSManagedObject *gameObject = [SCGameManager sharedInstance].gameObject;
int playerNumber = 0;
int totalOfPlayers = [(NSSet*)[gameObject valueForKey:@"playerColors"] count];
float anglePerPlayer = M_PI*2 / totalOfPlayers;
for (NSManagedObject *aPlayerColor in [gameObject valueForKey:@"playerColors"]){
//Draw the progress
CGFloat startAngle = anglePerPlayer * playerNumber;
CGFloat endAngle = startAngle + anglePerPlayer;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:self.frame. size.width/2 startAngle:startAngle endAngle:endAngle clockwise:YES];
UIColor *playerColor = [SCConstants getUIColorForPlayer:[[aPlayerColor valueForKey:@"colorIndex"] intValue]];
[playerColor set];
[path fill];
playerNumber++;
}
}
Run Code Online (Sandbox Code Playgroud)
显然,我只需要将我的Path移动到圆圈的中心,然后关闭它,但是当我添加以下代码行时:
[path addLineToPoint:self.center];
[path closePath];
Run Code Online (Sandbox Code Playgroud)
它吸引了一些奇怪的东西:

你知道我的代码出了什么问题吗?我根本不是Bezier的专家,所以欢迎任何帮助!
谢谢!
Cal*_*leb 12
看起来你正在使用的中心点就是问题.实际上,如果您查看centerUIView属性的文档,您会发现:
中心在其超视图的坐标系内指定,并以点为单位进行测量.
您希望在其自己的坐标系中指定视图的中心点,而不是其超级视图的中心点.您已经在此处确定了视图中心的坐标:
CGPoint center = CGPointMake((bounds.size.width/2.0), (bounds.size.height/2.0));
Run Code Online (Sandbox Code Playgroud)
因此,只要改变你使用从中心点点self.center来center,像这样:
[path addLineToPoint:center];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2895 次 |
| 最近记录: |