繁体   English   中英

带有不同颜色的六边形iOS

[英]Hexagon with different colors iOS

我正在尝试使六边形具有不同的颜色。 我创建了一个六边形形状,但是我不能为每条线赋予不同的颜色。 有什么办法吗? 通过这些后我也可以更改前2条线的颜色吗?

这是我的代码;

- (void)drawRect:(CGRect)rect
{

float polySize = self.frame.size.height/2;

CGFloat hexWidth = self.frame.size.width;
CGFloat hexHeight = self.frame.size.height;

CGPoint center;

//Start drawing polygon
center = CGPointMake(hexWidth/2, hexHeight/2);
NSLog(@"center x = %f",center.x);

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(center.x, 0)];
NSLog(@"center x = %f --- center y = %f",center.x,center.y + 100);

for(int i = 3; i >= 0 ; 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",center.x + x,center.y + y);

    //CGContextAddLineToPoint(context, center.x + x, center.y + y);
    [path addLineToPoint:CGPointMake(center.x + x, center.y + y)];

}

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);

    NSLog(@"x = %f --- y = %f",center.x + x,center.y + y);

    //CGContextAddLineToPoint(context, center.x + x, center.y + y);
    [path addLineToPoint:CGPointMake(center.x + x, center.y + y)];

}

CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.bounds;
pathLayer.path = path.CGPath;
pathLayer.lineCap = kCALineCapRound;

pathLayer.strokeColor = [[UIColor blackColor] CGColor];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 10.0f;
pathLayer.cornerRadius = 5.0f;
pathLayer.lineJoin = kCALineJoinBevel;
//pathLayer.lineDashPattern = @[@10];

[self.layer addSublayer:pathLayer];

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 0.8;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];

}

感谢您的帮助和关注。

形状层全部以一种颜色绘制。 使用CGGraphics绘制的UIBezierPath也是如此。 如果要使用混合色,则需要从多个形状图层或自定义drawRect中的多个绘制命令构造它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM