为app实现渐变的遮罩效果
2016-04-07 本文已影响612人
欣东
效果图如下
exampleImg1.png实现代码如下
//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];