我正在尝试使用 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++;
}
}
显然,我只需要将我的路径移动到圆心,然后将其关闭,但是当我添加以下代码行时:
[path addLineToPoint:self.center];
[path closePath];
它画了一些奇怪的东西:
你知道我的代码出了什么问题吗?我根本不是贝塞尔专家,所以欢迎任何帮助!
谢谢!