美文网首页
2019-10-25

2019-10-25

作者: 月禅 | 来源:发表于2019-10-25 13:35 被阅读0次

    iOS 渐变色

    实现渐变色的几种方式:

    使用场景:背景渐变

    1. 通过CAGradientLayer实现

    // 1. 重写UIView的方法, 返回一个渐变图层
    + (Class)layerClass {
        return [CAGradientLayer class];
    }
    -  (void)configGradientLayer {
      // 颜色cgcolor
      NSArray *colors =  @[(id)[UIColor redColor].CGColor,
                               (id)[UIColor greenColor].CGColor];
        self.layer.colors = colors;
        // 设置渐变的方向 矢量 
        self.layer.startPoint = CGPointMake(0, 0);
        self.layer.endPoint = CGPointMake(1.0, 0.0);
    }
    

    使用场景:背景渐变, 文字渐变

    2. 创建一个渐变的Image

    + (UIImage *)linearGradientImageWithSize:(CGSize)size
                                      colors:(NSArray<id>*)colors
                                   locations:(CGFloat[])locations
                                  startPoint:(CGPoint)startPoint
                                    endPoint:(CGPoint)endPoint
    {
        UIImage *resultImage = nil;
        //开启图片上下文
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        //创建路径
        UIBezierPath *path = [UIBezierPath bezierPathWithRect: CGRectMake(0,0,size.width,size.height)];
        //颜色
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
        //具体方向可根据需求修改
        CGContextSaveGState(context);
        CGContextAddPath(context, path.CGPath);
        CGContextClip(context);
        //画渐变
        CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
        CGContextRestoreGState(context);
        //资源释放
        CGGradientRelease(gradient);
        CGColorSpaceRelease(colorSpace);
        //生成图片
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return resultImage;
    }
    
    - (void)testButtonAddGradientBackground {
      NSArray *colors =  @[(id)[UIColor redColor].CGColor,
                               (id)[UIColor greenColor].CGColor];
        CGFloat locations[2] = {0.0, 1.0};
        UIImage *followImage = [self linearGradientImageWithSize:CGSizeMake(36, 36) colors:colors locations:locations startPoint:CGPointMake(0, 0) endPoint:CGPointMake(36, 0)];
    // 设置按钮的背景
        [button setBackgroundImage:followImage forState:UIControlStateNormal];
              [button setImage:image forState:UIControlStateNormal];
    }
    
    - (void)testLabelTextGradientColor {
              NSArray *colors =  @[(id)[UIColor redColor].CGColor,
                               (id)[UIColor greenColor].CGColor];
            CGFloat locations[2] = {0.0, 1.0};
            UIImage *gradientImage = [self linearGradientImageWithSize:CGSizeMake(30,30) colors:colors locations:locations startPoint:CGPointMake(0, 0) endPoint:CGPointMake(30, 0)];
            [self.titleLabel.textColor = [UIColor colorWithPatternImage: gradientImage];
    }
    

    相关文章

      网友评论

          本文标题:2019-10-25

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