美文网首页
iOS 上下渐变色+透明度处理

iOS 上下渐变色+透明度处理

作者: MrPlusZhao | 来源:发表于2021-02-05 16:12 被阅读0次
    Simulator Screen Shot - iPhone 8 - 2021-02-05 at 15.58.50.png

    Demo地址:
    https://github.com/MrPlusZhao/GradualColorDemo.git

    - (void)changeColor
    {
        UIColor *color0 = [[UIColor blueColor] colorWithAlphaComponent:0.0];
        UIColor *color1 = [[UIColor blueColor] colorWithAlphaComponent:0.6];
        [self colorGradientFrom:color0 toColor:color1 startP:CGPointMake(0, 0) EndP:CGPointMake(0, 1) SubView:self.xxView];
    }
    - (void)changeColor2
    {
        UIColor *color0 = [[UIColor redColor] colorWithAlphaComponent:0.6];
        UIColor *color1 = [[UIColor redColor] colorWithAlphaComponent:0.0];
        [self colorGradientFrom:color0 toColor:color1 startP:CGPointMake(0, 0) EndP:CGPointMake(0, 1) SubView:self.yyView];
    }
    /// 渐变色
    - (void)colorGradientFrom:(UIColor*)color0 toColor:(UIColor*)color1 startP:(CGPoint)point0 EndP:(CGPoint)point1 SubView:(UIView*)subView
    {
        //渐变设置
        CAGradientLayer *gradient = [CAGradientLayer layer];
        NSArray *colors = [NSArray arrayWithObjects:(id)color0.CGColor, (id)color1.CGColor, nil];
        //设置开始和结束位置(通过开始和结束位置来控制渐变的方向)
        gradient.startPoint = point0;
        gradient.endPoint = point1;
        gradient.colors = colors;
        gradient.locations = @[@(0.0),@(1.0f)];
        gradient.frame = CGRectMake(0, 0, subView.bounds.size.width, subView.bounds.size.height);
        [subView.layer insertSublayer:gradient atIndex:0];
    }
    

    相关文章

      网友评论

          本文标题:iOS 上下渐变色+透明度处理

          本文链接:https://www.haomeiwen.com/subject/frcgtltx.html