iOS|iOS CALayer mask 属性



/* 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(strong) CALayer *mask;


以上是CALayer的头文件关于mask的说明,mask实际上layer内容的一个遮罩。
如果我们把mask是透明的,实际看到的layer是完全透明的,也就是说只有mask的内容不透明的部分和layer叠加的部分才会显示出来,
效果如下



【iOS|iOS CALayer mask 属性】
func addShapeView() -> Void {let wdthLabel = createLabel(rect: CGRect.init(x: 100, y: 100, width: 100, height: 100), text: "这是一个label"); wdthLabel.textColor = UIColor.darkGray; wdthLabel.font = fontSize(size: 20); titlDb = createLabel(rect: CGRect.init(x: 100, y: 100, width: 100, height: 100), text: "这是一个label"); titlDb.textColor = UIColor.green; titlDb.font = fontSize(size: 20); //titlDb.backgroundColor = UIColor.orange; maskLayer = CATextLayer(); maskLayer.frame = CGRect.init(x: 0, y: 0, width: 100, height: 100); maskLayer.backgroundColor = UIColor.clear.cgColor; //maskLayer.string = "这是一个label"; maskLayer.foregroundColor = UIColor.blue.cgColor; maskLayer.fontSize = 20; maskLayer.backgroundColor = UIColor.blue.cgColor; titlDb.layer.mask = maskLayer; //label.layer.addSublayer(bcakItem); } var titlDb: UILabel! var maskLayer: CATextLayer!



    推荐阅读