iOS如何实现渐变按钮Gradient Button

这篇文章将为大家详细讲解有关iOS如何实现渐变按钮Gradient Button,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

GradientCategory

使用category实现gradient

效果图

iOS如何实现渐变按钮Gradient Button

来看.h文件

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, GradientType) {
 GradientFromTopToBottom = 1,   //从上到下
 GradientFromLeftToRight,    //从做到右
 GradientFromLeftTopToRightBottom,  //从上到下
 GradientFromLeftBottomToRightTop  //从上到下
};

@interface UIImage (Gradient)

/**
 * 根据给定的颜色,生成渐变色的图片
 * @param imageSize  要生成的图片的大小
 * @param colorArr   渐变颜色的数组
 * @param percents   渐变颜色的占比数组
 * @param gradientType  渐变色的类型
 */
- (UIImage *)createImageWithSize:(CGSize)imageSize gradientColors:(NSArray *)colorArr percentage:(NSArray *)percents gradientType:(GradientType)gradientType;

@end


#import <UIKit/UIKit.h>

#import "UIImage+Gradient.h"

@interface UIButton (Gradient)

/**
 * 根据给定的颜色,设置按钮的颜色
 * @param btnSize 这里要求手动设置下生成图片的大小,防止coder使用第三方layout,没有设置大小
 * @param clrs  渐变颜色的数组
 * @param percent 渐变颜色的占比数组
 * @param type  渐变色的类型
 */
- (UIButton *)gradientButtonWithSize:(CGSize)btnSize colorArray:(NSArray *)clrs percentageArray:(NSArray *)percent gradientType:(GradientType)type;

@end

关于“iOS如何实现渐变按钮Gradient Button”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。