iOS一行代码实现view渐变色

在app开发中,经常会给一些view加上渐变色,下面我写一下我在项目中的实现方法。

渐变色,其实就是一种颜色,所以就是UIColor的一种。因此我们可以给UIColor写一个分类,在需要用的地方直接使用类方法,得到我们想要的渐变色。

首先,我们创建一个UIColor的分类UIColor+Gradient,然后在.h文件中定义一个枚举,用来设置渐变的方向:

typedefNS_ENUM(NSInteger, ZQGradientChangeDirection) {

    ZQGradientChangeDirectionLevel,                               //水平方向渐变  

    ZQGradientChangeDirectionVertical,                           //垂直方向渐变

    ZQGradientChangeDirectionUpwardDiagonalLine,    //主对角线方向渐变

    ZQGradientChangeDirectionDownDiagonalLine,       //副对角线方向渐变

};

接着定义一个类初始化方法:

/*

          size:渐变区域的尺寸

  direction:渐变方向

 startColor:开始颜色

   endColor:结束颜色

 */

+ (instancetype)bm_colorGradientChangeWithSize:(CGSize)size

                                     direction:(IHGradientChangeDirection)direction

                                    startColor:(UIColor*)startcolor

                                      endColor:(UIColor*)endColor;

好了,现在去.m文件中实现就可以了,直接上代码:

+ (instancetype)bm_colorGradientChangeWithSize:(CGSize)size

                                     direction:(IHGradientChangeDirection)direction

                                    startColor:(UIColor*)startcolor

                                      endColor:(UIColor*)endColor {

   if(CGSizeEqualToSize(size,CGSizeZero) || !startcolor || !endColor) {

        return nil;

    }

   CAGradientLayer *gradientLayer = [CAGradientLayer layer];

    gradientLayer.frame=CGRectMake(0,0, size.width, size.height);


    CGPointstartPoint =CGPointZero;

    if (direction == IHGradientChangeDirectionDownDiagonalLine) {

        startPoint =CGPointMake(0.0,1.0);

    }

    gradientLayer.startPoint= startPoint;

   CGPoint endPoint = CGPointZero;

    switch(direction) {

        case IHGradientChangeDirectionLevel:

            endPoint =CGPointMake(1.0,0.0);

            break;

        case IHGradientChangeDirectionVertical:

            endPoint =CGPointMake(0.0,1.0);

            break;

        case IHGradientChangeDirectionUpwardDiagonalLine:

            endPoint =CGPointMake(1.0,1.0);

            break;

        case IHGradientChangeDirectionDownDiagonalLine:

            endPoint =CGPointMake(1.0,0.0);

            break;

        default:

            break;

    }

    gradientLayer.endPoint= endPoint;

   gradientLayer.colors=@[(__bridgeid)startcolor.CGColor, (__bridgeid)endColor.CGColor];

    UIGraphicsBeginImageContext(size);

    [gradientLayerrenderInContext:UIGraphicsGetCurrentContext()];

    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return [UIColor colorWithPatternImage:image];

}

好了,可以使用了,这是各个方向的效果图:

ZQGradientChangeDirectionLevel:


ZQGradientChangeDirectionLevel

ZQGradientChangeDirectionVertical:


ZQGradientChangeDirectionVertical

ZQGradientChangeDirectionUpwardDiagonalLine:


ZQGradientChangeDirectionUpwardDiagonalLine

ZQGradientChangeDirectionDownDiagonalLine:


ZQGradientChangeDirectionDownDiagonalLine
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容