Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 设置CALayer maskToBounds导致图层消失_Ios_Objective C_Uiview_Calayer_Cashapelayer - Fatal编程技术网

Ios 设置CALayer maskToBounds导致图层消失

Ios 设置CALayer maskToBounds导致图层消失,ios,objective-c,uiview,calayer,cashapelayer,Ios,Objective C,Uiview,Calayer,Cashapelayer,我试图创建一个圆形,它是根据一定的百分比填充的。下面是我想要的效果图片: 我有一个UIView,我添加了一个绘制圆的CAShapeLayer。然后,我将创建另一个UIShapeLayer作为正方形,以匹配包含圆的UIView。然后根据图形设置这个的高度,如果正方形是100px高,值是10%,那么我将正方形设置为10px,这样它就填充了圆圈的10% 下图显示了50%的圆被填充。正如您所见,它覆盖了UIView和圆形。但是,当我尝试将CAShapeLayer设置为“蒙版到边界”(circle.ma

我试图创建一个圆形,它是根据一定的百分比填充的。下面是我想要的效果图片:

我有一个UIView,我添加了一个绘制圆的CAShapeLayer。然后,我将创建另一个UIShapeLayer作为正方形,以匹配包含圆的UIView。然后根据图形设置这个的高度,如果正方形是100px高,值是10%,那么我将正方形设置为10px,这样它就填充了圆圈的10%

下图显示了50%的圆被填充。正如您所见,它覆盖了UIView和圆形。但是,当我尝试将CAShapeLayer设置为“蒙版到边界”(circle.maskToBounds)时,完整的圆将随着我添加为其子视图的正方形一起消失

这是我的密码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    [self drawCircleInView:view];
    [self.view addSubview:view];
}

我想知道为什么设置circle.masksToBounds=YES会使圆和子层完全消失。我的理解是,通过设置它应该只显示圆上的子层


非常感谢。

要实现这一点,请使用中心圆为透明的正方形图像。喜欢这个图像吗

如何使用此图像实现此效果-

首先添加方形层(颜色较浅/或非填充颜色)。在上面添加一个填充颜色为sane rect的图层。最重要的是,把这张照片放在这里。并更改具有填充颜色的中间层框架以达到所需效果

尝试以下操作:

- (void)drawCircleInView:(UIView *)v {
    // Set up the shape of the circle
    CGSize size = v.bounds.size;
    CALayer *layer = [CALayer layer];
    layer.frame = v.bounds;
    layer.backgroundColor = [UIColor blueColor].CGColor;

    CALayer *sublayer = [CALayer layer];
    sublayer.frame = CGRectMake(0, size.height/2, size.width, size.height/2);
    sublayer.backgroundColor = [UIColor orangeColor].CGColor;
    sublayer.opacity = .5f;

    CAShapeLayer *mask = [CAShapeLayer layer];
    mask.frame = v.bounds;
    mask.path = [UIBezierPath bezierPathWithOvalInRect:v.bounds].CGPath;
    mask.fillColor = [UIColor blackColor].CGColor;
    mask.strokeColor = [UIColor clearColor].CGColor;

    [layer addSublayer:sublayer];
    [layer setMask:mask];

    // Add to parent layer
    [v.layer addSublayer:layer];
}
  • maskToBounds
    是“到边界”您没有设置
    边界
  • 这不是必需的,但您最好设置
    CAShapeLayer
    frame
    ,以防止不必要的混淆
  • bounds
    是矩形,而不是
    CAShapeLayer
    的形状。使用
    遮罩
    层以按形状遮罩
  • 子层
    不需要是
    现金流层
    ,因为它将被形状的
    蒙版
    层遮罩

  • 在你的圆圈图层上设置一个
    maskLayer
    怎么样?它认为这是一个更好的方法。谢谢这个rintaro。它工作得很好。谢谢你的解释,谢谢你的建议。我用rintaro的答案作为背景,这些圆圈将显示在一个图案上。根据我的理解,只有当图像中不透明的部分与背景匹配时,这才有效。请使用最适合您的部分:)
    - (void)drawCircleInView:(UIView *)v {
        // Set up the shape of the circle
        CGSize size = v.bounds.size;
        CALayer *layer = [CALayer layer];
        layer.frame = v.bounds;
        layer.backgroundColor = [UIColor blueColor].CGColor;
    
        CALayer *sublayer = [CALayer layer];
        sublayer.frame = CGRectMake(0, size.height/2, size.width, size.height/2);
        sublayer.backgroundColor = [UIColor orangeColor].CGColor;
        sublayer.opacity = .5f;
    
        CAShapeLayer *mask = [CAShapeLayer layer];
        mask.frame = v.bounds;
        mask.path = [UIBezierPath bezierPathWithOvalInRect:v.bounds].CGPath;
        mask.fillColor = [UIColor blackColor].CGColor;
        mask.strokeColor = [UIColor clearColor].CGColor;
    
        [layer addSublayer:sublayer];
        [layer setMask:mask];
    
        // Add to parent layer
        [v.layer addSublayer:layer];
    }