@interface ViewController () @property (nonatomic, weak) IBOutlet UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //create mask layer CALayer *maskLayer = [CALayer layer]; maskLayer.frame = self.layerView.bounds; UIImage *maskImage = [UIImage imageNamed:@"Cone.png"]; // 带有alpha值的图片 maskLayer.contents = (__bridge id)maskImage.CGImage; //apply mask to image layer? self.imageView.layer.mask = maskLayer; // 需要蒙版效果的图片 } @end
CALayer蒙板图层真正厉害的地方在于蒙板图不局限于静态图。任何有图层构成的都可以作为mask
属性,这意味着你的蒙板可以通过代码甚至是动画实时生成。
原文地址:https://www.cnblogs.com/Caersi/p/11183273.html
时间: 2024-10-11 01:19:25