mask layer的遮罩层

时间:2022-08-07 19:34:16

 

1. layer层 mask 遮罩效果

 //渐变层
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 100, kWidth, kWidth);
    gradientLayer.colors = @[(__bridge id)[[UIColor redColor]colorWithAlphaComponent:0.4] .CGColor,
                             (__bridge id)[UIColor clearColor].CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint  = CGPointMake(1, 0);
    
    [self.view.layer addSublayer:gradientLayer];
    
    
    //
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = self.view.bounds;
    layer.lineWidth = 5;
    layer.strokeColor = [UIColor redColor].CGColor;
    layer.fillColor = [UIColor redColor].CGColor;
    layer.path = path.CGPath;
    layer.lineCap = @"round";
//    [gradientLayer addSublayer:layer];
    
    gradientLayer.mask = layer;
    

 

2. maskView 实现局部透明效果

//0.相当于maskView 将自己"投影"到 view上, 注意层级关系, 实际并不是在'灰色'的view上滑动, 而是投影到了"灰色"的view上了

//1.设置了遮罩mask属性后, 只显示重叠部分

//2.可以通过改变遮罩的alpha和颜色实现透明、半透明的效果

   UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:self.view.bounds];
    imageView1.image = [UIImage imageNamed:@"1"];
    [self.view addSubview:imageView1];
    
    TestView *view = [TestView new];
    view.frame = self.view.bounds;
    view.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.9];
    [self.view addSubview:view];
 
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"1"];
    [self.view addSubview:imageView];
    
    
    UIView *roundView = [[UIView alloc]initWithFrame: CGRectMake(50, 50, 100, 100)];
    roundView.backgroundColor = [UIColor redColor];
    _viewwww = roundView;
    imageView.maskView = roundView;