iOS 渐变色

UIColor (ChangingColor)


.h文件

//绘制渐变色颜色的方法

+ (UIImage*)setGradualChangingColor:(UIView*)view fromColor:(NSString*)fromHexColorStr toColor:(NSString*)toHexColorStr;


.m文件

//绘制渐变色颜色的方法

+ (UIImage*)setGradualChangingColor:(UIView*)view fromColor:(NSString*)fromHexColorStr toColor:(NSString*)toHexColorStr{


    //    CAGradientLayer类对其绘制渐变背景颜色、填充层的形状(包括圆角)

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];

    gradientLayer.frame= view.bounds;


    //  创建渐变色数组,需要转换为CGColor颜色

    gradientLayer.colors=@[(__bridgeid)[UIColorcolorWithHexString:fromHexColorStr].CGColor,(__bridgeid)[UIColorcolorWithHexString:toHexColorStr].CGColor];


    //  设置渐变颜色方向,左上点为(0,0), 右下点为(1,1)

    gradientLayer.startPoint=CGPointMake(0,0);

    gradientLayer.endPoint=CGPointMake(1,1);


    //  设置颜色变化点,取值范围 0.0~1.0

    gradientLayer.locations=@[@0,@1];


    UIGraphicsBeginImageContextWithOptions(gradientLayer.frame.size, NO, 0);


    [gradientLayerrenderInContext:UIGraphicsGetCurrentContext()];

    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();


    UIGraphicsEndImageContext();


    returnoutputImage;

}

你可能感兴趣的:(iOS 渐变色)