为app实现渐变的遮罩效果

效果图如下

实现代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//blackView为你想加上遮罩的视图


   CGColorRef opaqueBlackColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor;
    CGColorRef transparentBalckColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4].CGColor;

       //遮罩效果由CAGradientLayer实现
    CAGradientLayer * layer = [[CAGradientLayer alloc]init];
    layer.frame = CGRectMake(0, 0, WIDTH, self.blackView.bounds.size.height);

    //设置渐变的方向
    layer.startPoint = CGPointMake(0, 1);
    layer.endPoint = CGPointMake(0, 0);

    //设置渐变得颜色范围
    layer.colors = @[(__bridge id)opaqueBlackColor,(__bridge id)transparentBalckColor];

    [self.blackView.layer insertSublayer:layer atIndex:0];

Comments