CAShapeLayer strokeColor与fillColor混合使用

时间:2014-08-22 12:19:09

标签: ios7 quartz-core cashapelayer

我制作了带边框的六角形视图。我现在面临的问题是我无法使用strokeWidth(而不是borderWidth)设置所需的边框颜色,现在我的图层的strokeColor与图层的填充颜色混合。

以下是代码:

CAShapeLayer *hexagonMask = [CAShapeLayer layer];
CAShapeLayer *hexagonBorder = [CAShapeLayer layer];
hexagonMask.frame=self.layer.bounds;
hexagonBorder.frame = self.layer.bounds;

UIBezierPath* hexagonPath=[self makeHexagonalMaskPathWithSquareSide:side];     //making hexagon path

hexagonMask.path = hexagonPath.CGPath;                 //setting the mask path for hexagon
hexagonBorder.path = hexagonPath.CGPath;               //setting the maskpath for hexagon border
hexagonBorder.fillColor=[UIColor clearColor].CGColor; //setting by default color

hexagonBorder.lineWidth = self.customBorderWidth; //   setting border width

hexagonBorder.strokeColor = self.customBorderColor.CGColor;  //setting  color for hexagon border 

hexagonBorder.fillColor = self.customFillColor.CGColor;    //setting fill color for hexagon     

有任何解决此问题的建议吗?

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决这个问题的方法。 我添加了一个CALayer作为子图层,填充颜色用于创建UIImage,用于设置子图层的内容。 以下是可能在将来遇到此问题的人的代码

CAShapeLayer *hexagonMask = [CAShapeLayer layer];
CAShapeLayer *hexagonBorder = [CAShapeLayer layer];
hexagonMask.frame=self.layer.bounds;
hexagonBorder.frame = self.layer.bounds;

UIBezierPath* hexagonPath=[self makeHexagonalMaskPathWithSquareSide:side];       //making hexagon path

hexagonMask.path = hexagonPath.CGPath;                 //setting the mask path for   hexagon
hexagonBorder.path = hexagonPath.CGPath;               //setting the maskpath for hexagon border
hexagonBorder.fillColor=[UIColor clearColor].CGColor; //setting by default color

hexagonBorder.lineWidth = self.customBorderWidth; //   setting border width

hexagonBorder.strokeColor = self.customBorderColor.CGColor;  //setting  color for hexagon border 


CGFloat coverImageSide=side-(_customBorderWidth);
            CGFloat backgroundLayerXY=_customBorderWidth/2.0;

            UIImage *coverImage=[UIImage ImageWithColor:_customFillColor width:coverImageSide height:coverImageSide];
            UIBezierPath* dpath=[self makeHexagonalMaskPathWithSquareSide:coverImageSide];


            CALayer *backgroundLayer=[CALayer layer];
            backgroundLayer.frame=CGRectMake(backgroundLayerXY, backgroundLayerXY, coverImageSide, coverImageSide);
            CAShapeLayer *backgroundMaskLayer=[CAShapeLayer layer];
            backgroundMaskLayer.path=dpath.CGPath;
            backgroundLayer.mask=backgroundMaskLayer;
            [backgroundLayer setContentsScale:[UIScreen mainScreen].scale];
            [backgroundLayer setContentsGravity:kCAGravityResizeAspectFill];
            [backgroundLayer setContents:(__bridge id)[UIImage maskImage:coverImage toPath:dpath].CGImage];

            [self.layer.sublayers[0] addSublayer:backgroundLayer];