IOS: 无法更改 CAShapeLayer 的颜色
IOS: Cannot Change Color of CAShapeLayer
我想创建一个层作为我在界面生成器中创建的 UIImageView 的遮罩。但是,无论我做什么,该层都保持白色。代码非常简单,知道是什么导致了这种行为吗?
UIImage* image = [UIImage imageNamed:@"face"];
self.imageView.image = image;
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = self.imageView.bounds;
maskLayer.fillColor = [[UIColor blackColor] CGColor];
maskLayer.path = CGPathCreateWithRect(self.imageView.bounds, NULL);
self.imageView.layer.mask = maskLayer;
self.maskLayer = maskLayer;
我修改了代码,添加了路径,还是不行。
你好像对什么是口罩有误解。实际上,它是一组指令,用于将透明度注入视图,因此 "punching a hole" 或多或少地通过视图(取决于透明度)。它没有颜色。您看到白色是因为那是图像视图后面的颜色 — 您在 整个 图像视图中打了一个洞并使其不可见。
The layer’s alpha channel determines how much of the layer’s content
and background shows through. Fully or partially opaque pixels allow
the underlying content to show through but fully transparent pixels
block that content.
黑色填充(闭合路径的内部)= 不透明像素
封闭路径外的区域 = 透明像素
图像将出现在路径的内部区域,不会出现在该路径之外。实际颜色无关紧要:当您将图层设置为蒙版时,我们现在只关心不透明度。
路径表面上与 imageView 的大小相同,因此您不会看到任何差异,因为掩码与 imageView 边界匹配。此外,如果您在几何完全设置之前使用此代码,例如在 viewDidLoad
中,您可能无法获得预期的结果。
正如马特所建议的那样 - 你需要考虑你想要的结果。
如果您想要 "black translucent color" - 或类似效果 - 考虑在混合物中添加另一个半透明色层。但这不是面具。
我想创建一个层作为我在界面生成器中创建的 UIImageView 的遮罩。但是,无论我做什么,该层都保持白色。代码非常简单,知道是什么导致了这种行为吗?
UIImage* image = [UIImage imageNamed:@"face"];
self.imageView.image = image;
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = self.imageView.bounds;
maskLayer.fillColor = [[UIColor blackColor] CGColor];
maskLayer.path = CGPathCreateWithRect(self.imageView.bounds, NULL);
self.imageView.layer.mask = maskLayer;
self.maskLayer = maskLayer;
我修改了代码,添加了路径,还是不行。
你好像对什么是口罩有误解。实际上,它是一组指令,用于将透明度注入视图,因此 "punching a hole" 或多或少地通过视图(取决于透明度)。它没有颜色。您看到白色是因为那是图像视图后面的颜色 — 您在 整个 图像视图中打了一个洞并使其不可见。
The layer’s alpha channel determines how much of the layer’s content and background shows through. Fully or partially opaque pixels allow the underlying content to show through but fully transparent pixels block that content.
黑色填充(闭合路径的内部)= 不透明像素
封闭路径外的区域 = 透明像素
图像将出现在路径的内部区域,不会出现在该路径之外。实际颜色无关紧要:当您将图层设置为蒙版时,我们现在只关心不透明度。
路径表面上与 imageView 的大小相同,因此您不会看到任何差异,因为掩码与 imageView 边界匹配。此外,如果您在几何完全设置之前使用此代码,例如在 viewDidLoad
中,您可能无法获得预期的结果。
正如马特所建议的那样 - 你需要考虑你想要的结果。
如果您想要 "black translucent color" - 或类似效果 - 考虑在混合物中添加另一个半透明色层。但这不是面具。