如果将掩码分配给作为子图层的图层,则掩码不起作用

时间:2017-08-25 03:44:58

标签: ios objective-c uikit

得到了一个正确添加和定位的_imageView

如果将一个图层分配给_imageView.layer.mask,它可以工作:

CAShapeLayer *maskLayer = [CAShapeLayer layer];

[maskLayer setPath:maskPath;

[maskLayer setPosition:CGPointMake(50, 50)]

[_imageViewLayer setMask:maskLayer];

现在,掩码会阻止其路径之外的内容

但是,如果将一个图层分配给_imageView.layer.sublayers之一,它会阻止所有内容:

_imageViewLayer = [CAShapeLayer layer];

CGPathRef circlePath = CGPathCreateWithEllipseInRect(CGRectMake(0, 0, 100, 100), nil);

[_imageViewLayer setPath: circlePath];

[_imageViewLayer setPosition: CGPoinMake(50, 50)];

CGPathRelease(circlePath);

[_imageViewLayer setFillColor:[[UIColor redColor] CGColor]];

[[_imageView layer] addSublayer:_imageViewLayer];


CAShapeLayer *maskLayer = [CAShapeLayer layer];

CGPathRef maskPath = CGPathCreateWithEllipseInRect(CGRectMake(0, 0, 10, 10), nil);

[maskLayer setPath:maskPath];

CGPathRelease(maskPath);

[maskLayer setPosition: CGPoinMake(50, 50)];

[_imageViewLayer setMask: maskLayer];

如果我删除了maskLayer,我可以在_imageView上看到一个红色圆圈

任何人都可以给我一个提示,或者它只是一个错误?

1 个答案:

答案 0 :(得分:0)

来自Apple:

/* A layer whose alpha channel is used as a mask to select between the
 * layer's background and the result of compositing the layer's
 * contents with its filtered background. Defaults to nil. When used as
 * a mask the layer's `compositingFilter' and `backgroundFilters'
 * properties are ignored. When setting the mask to a new layer, the
 * new layer must have a nil superlayer, otherwise the behavior is
 * undefined. Nested masks (mask layers with their own masks) are
 * unsupported. */

@property(nullable, strong) CALayer *mask;

“将掩码设置为新图层时,新图层必须具有nil超级图层,否则行为未定义。”

当您将_imageViewLayer设置为[_imageView layer]作为掩码时,maskLayer已经有了一个超级_imageViewLayer