【问题标题】:CAShapeLayer strokeColor get mix with fillColorCAShapeLayer strokeColor 与 fillColor 混合
【发布时间】:2014-08-22 12:19:09
【问题描述】:

我制作了一个带边框的六边形视图。我现在面临的问题是我无法为使用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     

有解决这个问题的建议吗?

【问题讨论】:

    标签: ios7 quartz-core cashapelayer


    【解决方案1】:

    我终于找到了解决这个问题的方法。 我添加了一个 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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多