Mask Property An optional layer whose alpha channel is used to mask the layer’s content.
- Declaration
Copy the code
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, ** *Availability** Available in iOS 3.0 and later. ---- The above is the description of **Mask** in Apple's official document. It is not difficult to see that Mask does not exist in general and defaults to nil.**Mask** is equivalent to a cover covering the upper Layer of the view Layer Opacity (=alpha), 'bounds' and' <path> path 'of Mask** control the display scope and transparency of' ContentLayer '* **Mask** inside the 'ContentLayer' will not be displayed. *Mask** inside the 'ContentLayer' = Opacity ** *Mask** is also shown on opacity ** while opacity ** is 0 or 1, while opacity ** is 0. X Note in particular that **Mask** whose background color is' clearColor 'will also be considered when' opacity 'is 0 ** The official document warns that when you set a value for a Mask, it should be noted that the Mask cannot have a superLayer, otherwise the Mask will fail, resulting in unexpected consequences ** ---- ** If you are savvy, you may notice that Mask does not specify a layer type. In the outline, we already know that there are many types of CALayer, so that some special layers can be used to achieve some novel animation effects :** * ** This is a lens-like effect **! [MaskFilter.gif](http://upload-images.jianshu.io/upload_images/1594222-76762237b597cdb5.gif? imageMogr2/auto-orient/strip) ```swift 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, Opacity = 0.5 shaplayer. position = CGPointMake(100)) shaplayer. opacity = 0.5 shaplayer. position = CGPointMake(100)) 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. } }Copy the code
# # # # # extension:
- Use another layer (
CAGradientLayer
) can implement more cool effects, such as the iPhone’s left swipe to unlock the font highlighting, which will be detailed in a later articleCAGradientLayer
A summary of