用CALayer实现聚光灯效果

效果图:

用CALayer实现聚光灯效果

代码部分:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) CALayer *imageLayer;
@property (nonatomic, strong) CALayer *maskViewLayer; @property (nonatomic, strong) UIImage *contentImage;
@property (nonatomic, strong) UIImage *maskViewContentImage; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; self.contentImage = [UIImage imageNamed:@"原始图片"];
self.maskViewContentImage = [UIImage imageNamed:@"maskLayerContents"]; self.imageLayer = [CALayer layer];
self.imageLayer.frame = CGRectMake(, , , );
self.imageLayer.contents = (id)self.contentImage.CGImage; self.maskViewLayer = [CALayer layer];
self.maskViewLayer.frame = CGRectMake(-, -, , );
self.maskViewLayer.contents = (id)self.maskViewContentImage.CGImage; // self.imageLayer的局部区域的显示与否可以通过mask的alpha通道来决定
self.imageLayer.mask = self.maskViewLayer; [self.view.layer addSublayer:self.imageLayer]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
self.maskViewLayer.frame = self.imageLayer.bounds;
}

github:https://github.com/RinpeChen/maskViewInCALayer

上一篇:SpringBoot应用的启动方式


下一篇:mysql--sqlalchemy.exc.IntegrityError: (IntegrityError) (1215, 'Cannot add foreign key constraint'