iOS CALayer mask 屬性
阿新 • • 發佈:2018-11-25
/* 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疊加的部分才會顯示出來,
效果如下
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!