mask Property
An optional layer whose alpha channel is used to mask the layer’s content.
- Declaration
SWIFT
var mask: CALayer?
* **Discussion**
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.
The default value of this property is nil nil. When configuring a mask, remember to set the size and position of the mask layer to ensure it is aligned properly with the layer it masks.
* **Special Considerations**
The layer you assign to this property must not have a superlayer. If it does, the behavior is undefined.
* **Availability**
Available in iOS 3.0 and later.
----
以上是**Mask**在Apple的的官方文檔中的描述,不難看出Mask在一般情況下是不存在,默認(rèn)是nil.**Mask**相當(dāng)于一個(gè)遮蓋,覆蓋在視圖Layer的上層,如果將視圖Layer稱之為`ContentLayer`,可以通過控制**Mask**的`opacity(=alpha)`,`bounds`和`<path>路徑`來控制`ContentLayer`的顯示范圍和透明度
* 在**Mask**范圍之外的`ContentLayer`將不被顯示(相當(dāng)于沒有繪制在屏幕上,可以看到`ContentLayer`背后的視圖)
* 在**Mask**范圍之內(nèi)的`ContentLayer = ContentLayer的opacity * **Mask**的opacity`,也就意味著**Mask**的`opacity`為0或1時(shí)`ContentLayer`顯示或不顯示,當(dāng)其為0.x時(shí)`ContentLayer`為半透明狀態(tài)
* 特別需要注意的是,**Mask**的背景色為`clearColor`也會(huì)被認(rèn)為時(shí)`opacity`為0
**官方文檔中已經(jīng)警告:當(dāng)你給Mask賦值時(shí)需要注意Mask不能擁有superLayer,否者M(jìn)ask將會(huì)失效產(chǎn)生意想不到的后果**
----
**機(jī)智的小伙伴們可以發(fā)現(xiàn)Mask并沒有指定圖層類型,在大綱中,我們已經(jīng)知道了CALayer的類型有很多種,這樣的話就可以利用一些特殊的圖層實(shí)現(xiàn)一些新奇的動(dòng)畫效果:**
* **此為一個(gè)類似于透鏡的效果**

import UIKit
class ViewController: UIViewController {
var myImageView:UIImageView!
var maskLayer:CAShapeLayer!
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
func setupUI() {
let image = UIImage(named: "_snqARKTgoc")
let inputImage = CIImage(CGImage: image!.CGImage!)
let filter = CIFilter(name: "CIColorControls")
filter?.setValue(inputImage, forKey: "inputImage")
filter?.setValue(0.5, forKey: "inputBrightness")
let outputImage = UIImage(CIImage: filter!.outputImage!)
let bgImageView = UIImageView()
bgImageView.image = outputImage
bgImageView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * (image!.size.height/image!.size.width));
bgImageView.center = CGPointMake(self.view.center.x, self.view.center.y - 1)
self.view.addSubview(bgImageView)
let imageView = UIImageView()
imageView.image = image
imageView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * (image!.size.height/image!.size.width));
myImageView = imageView
imageView.center = self.view.center
self.view.addSubview(imageView)
let shapLayer = CAShapeLayer()
let path = CGPathCreateMutable()
maskLayer = shapLayer
shapLayer.bounds = CGRectMake(0, 0, 100, 100)
CGPathAddEllipseInRect(path, nil, CGRectMake(0, 0, 100, 100))
shapLayer.opacity = 0.5
shapLayer.position = CGPointMake(200, 100)
shapLayer.path = path
shapLayer.fillColor = UIColor.blackColor().CGColor
imageView.layer.mask = shapLayer
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let touchPoint = touch.locationInView(self.view)
if self.myImageView.frame.contains(touchPoint) {
let realPoint = touch.locationInView(self.myImageView)
maskLayer.position = realPoint
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
----
#####擴(kuò)展:
* **使用其他的圖層(`CAGradientLayer`)可以實(shí)現(xiàn)更多炫酷的效果,如iPhone的左滑解鎖字體高亮,詳細(xì)的實(shí)現(xiàn)方式會(huì)在后續(xù)的篇幅中結(jié)合`CAGradientLayer`一塊總結(jié)**