美文网首页
iOS按钮渐变色

iOS按钮渐变色

作者: happycheng | 来源:发表于2021-06-25 09:52 被阅读0次
    使用示例
       UIColor *color1 = UIColorForRGB(0xF46729);
        UIColor *color2 = UIColorForRGB(0xFFAC65);
        UIImage *bgImg = [UIImage gradientColorImageFromColors:@[color1, color2] gradientType:GradientTypeLeftToRight imgSize:self.rightBtn.size];
        [self.rightBtn setBackgroundColor:[UIColor colorWithPatternImage:bgImg]];
    
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    typedef NS_ENUM(NSUInteger, GradientType) {
    
    GradientTypeTopToBottom = 0,//从上到下
    
    GradientTypeLeftToRight = 1,//从左到右
    
    GradientTypeUpleftToLowright = 2,//左上到右下
    
    GradientTypeUprightToLowleft = 3,//右上到左下
    
    };
    
    @interface UIImage(GradientColor)
    
    + (UIImage *)gradientColorImageFromColors:(NSArray*)colors gradientType:(GradientType)gradientType imgSize:(CGSize)imgSize;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    
    
    #import "UIImage+GradientColor.h"
    
    @implementation UIImage(GradientColor)
    
    + (UIImage *)gradientColorImageFromColors:(NSArray*)colors gradientType:(GradientType)gradientType imgSize:(CGSize)imgSize {
    
        NSMutableArray *ar = [NSMutableArray array];
    
        for(UIColor *c in colors) {
    
            [ar addObject:(id)c.CGColor];
    
        }
    
        UIGraphicsBeginImageContextWithOptions(imgSize, YES, 1);
    
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        CGContextSaveGState(context);
    
        CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    
        CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    
        CGPoint start;
    
        CGPoint end;
    
        switch (gradientType) {
    
                case GradientTypeTopToBottom:
    
                start = CGPointMake(0.0, 0.0);
    
                end = CGPointMake(0.0, imgSize.height);
    
                break;
    
                case GradientTypeLeftToRight:
    
                start = CGPointMake(0.0, 0.0);
    
                end = CGPointMake(imgSize.width, 0.0);
    
                break;
    
                case GradientTypeUpleftToLowright:
    
                start = CGPointMake(0.0, 0.0);
    
                end = CGPointMake(imgSize.width, imgSize.height);
    
                break;
    
                case GradientTypeUprightToLowleft:
    
                start = CGPointMake(imgSize.width, 0.0);
    
                end = CGPointMake(0.0, imgSize.height);
    
                break;
    
                default:
    
                break;
    
            }
    
        CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
        CGGradientRelease(gradient);
    
        CGContextRestoreGState(context);
    
        CGColorSpaceRelease(colorSpace);
    
        UIGraphicsEndImageContext();
    
        return image;
    
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:iOS按钮渐变色

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