iOS - 关于蒙版渐变的实现

文/学无止境吧(简书作者)
原文链接:http://www.jianshu.com/p/a389207bc48c


有个需求,视图的右边需要渐隐,如下图:(这里只是随便拿个界面做下测试)


要求整个view渐渐alpha变透明,包括view上的子view,需要一起变透明。

拿起键盘就是干:

-(void)testAlpha{
    [self.view layoutIfNeeded];
    UIColor *color1 = [UIColor colorWithRed:(0)  green:(0)  blue:(0)   alpha:1.0];
    UIColor *color2 = [UIColor colorWithRed:(0)  green:(0)  blue:(0)  alpha:0.8];
    UIColor *color3 = [UIColor colorWithRed:(0)  green:(0)  blue:(0)  alpha:0.2];
    NSArray *colors = [NSArray arrayWithObjects:(id)color1.CGColor, color2.CGColor,color3.CGColor, nil];
    NSArray *locations = [NSArray arrayWithObjects:@(0.0), @(0.7),@(1.0), nil];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = colors;
    gradientLayer.locations = locations;
    gradientLayer.frame = _inputView.bounds;
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint   = CGPointMake(1, 0);
    _inputView.layer.mask = gradientLayer;
}

搞定!




展开阅读全文

没有更多推荐了,返回首页