在iOS中绘制类似于圆形图的Hexagon

我正在绘制一个有三种不同颜色的六边形。 我为每一行都给了一个颜色。

这是我的代码;

- (void)drawRect:(CGRect)rect { self.colors = [[NSMutableArray alloc] initWithObjects:[UIColor yellowColor],[UIColor yellowColor],[UIColor blueColor],[UIColor blueColor],[UIColor greenColor],[UIColor greenColor], nil]; [self addPointsToArray]; CGPoint startPoint = [[self.points objectAtIndex:self.pointCounter] CGPointValue]; CGPoint endPoint = [[self.points objectAtIndex:self.pointCounter+1] CGPointValue]; UIColor *color = [self.colors objectAtIndex:self.pointCounter]; [self drawingEachLineWithDifferentBezier:startPoint endPoint:endPoint color:color]; self.pointCounter++; } - (void)addPointsToArray { self.points = [[NSMutableArray alloc] init]; float polySize = self.frame.size.height/2; CGFloat hexWidth = self.frame.size.width; CGFloat hexHeight = self.frame.size.height; CGPoint center = CGPointMake(hexWidth/2, hexHeight/2); CGPoint startPoint = CGPointMake(center.x, 0); for(int i = 3; i >= 1 ; i--) { CGFloat x = polySize * sinf(i * 2.0 * M_PI / 6); CGFloat y = polySize * cosf(i * 2.0 * M_PI / 6); NSLog(@"x = %f, y= %f",x,y); CGPoint point = CGPointMake(center.x + x, center.y + y); [self.points addObject:[NSValue valueWithCGPoint:point]]; } for(int i = 6; i > 3 ; i--) { CGFloat x = polySize * sinf(i * 2.0 * M_PI / 6); CGFloat y = polySize * cosf(i * 2.0 * M_PI / 6); CGPoint point = CGPointMake(center.x + x, center.y + y); [self.points addObject:[NSValue valueWithCGPoint:point]]; } [self.points addObject:[NSValue valueWithCGPoint:startPoint]]; } - (void)drawingEachLineWithDifferentBezier:(CGPoint)startPoint endPoint:(CGPoint)endPoint color:(UIColor *)color { UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:startPoint]; [path addLineToPoint:endPoint]; CAShapeLayer *pathLayer = [CAShapeLayer layer]; pathLayer.frame = self.bounds; pathLayer.path = path.CGPath; pathLayer.lineCap = kCALineCapRound; //pathLayer.lineCap = kCALineCapSquare; pathLayer.strokeColor = [color CGColor]; pathLayer.fillColor = nil; pathLayer.lineWidth = 15.0f; pathLayer.cornerRadius = 2.0f; pathLayer.lineJoin = kCALineJoinBevel; //pathLayer.lineDashPattern = @[@15]; [self.layer addSublayer:pathLayer]; CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; pathAnimation.duration = 0.3f; pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; pathAnimation.toValue = [NSNumber numberWithFloat:1.0f]; pathAnimation.delegate = self; [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; } - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{ if (self.pointCounter < self.points.count - 1) { CGPoint startPoint = [[self.points objectAtIndex:self.pointCounter] CGPointValue]; CGPoint endPoint = [[self.points objectAtIndex:self.pointCounter+1] CGPointValue]; UIColor *color = [self.colors objectAtIndex:self.pointCounter]; [self drawingEachLineWithDifferentBezier:startPoint endPoint:endPoint color:color]; self.pointCounter++; } } 

我有这个看法

在这里输入图像说明

我的目的是,我想要变成黄色,直到红线指向3号线。 所以我想如果我能find红点coodinate,我可以在我的点数组中添加新的点。 比从2行结束到3行结束的红色点和蓝色行可以绘制黄色线。

我错了吗? 如果我不是,我怎么find红点坐标或你的build议是什么?

感谢您的回答和利益:)。