• 用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(0, 0, 200, 200);
        self.imageLayer.contents = (id)self.contentImage.CGImage;
        
        self.maskViewLayer = [CALayer layer];
        self.maskViewLayer.frame = CGRectMake(-200, -200, 200, 200);
        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

  • 相关阅读:
    禁止鼠标多次点击选中div中的文字
    深入浅出 Nodejs 学习笔记 1
    svn 树冲突
    nodejs 按行读取 readline
    git版本控制器的基本使用
    规范javascript书写
    media query
    软件工程
    The sixth day
    The fifth day
  • 原文地址:https://www.cnblogs.com/Rinpe/p/5147617.html
Copyright © 2020-2023  润新知