├── GradientButton ├── Assets.xcassets │ ├── Contents.json │ ├── right.imageset │ │ ├── 矩形-4-拷贝-2@2x.png │ │ └── Contents.json │ ├── rightArrow.imageset │ │ ├── rightArrow.png │ │ ├── rightArrow@2x.png │ │ └── Contents.json │ ├── gradientButton.imageset │ │ ├── gradientButton@2x.png │ │ └── Contents.json │ └── AppIcon.appiconset │ │ └── Contents.json ├── ViewController.h ├── AppDelegate.h ├── main.m ├── UIButton+Gradient.h ├── UIButton+Gradient.m ├── UIImage+Gradient.h ├── Info.plist ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── AppDelegate.m ├── UIImage+Gradient.m └── ViewController.m ├── GradientButton.xcodeproj ├── xcuserdata │ └── tb.xcuserdatad │ │ ├── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist │ │ └── xcschemes │ │ ├── xcschememanagement.plist │ │ └── GradientButton.xcscheme ├── project.xcworkspace │ ├── xcuserdata │ │ └── tb.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── contents.xcworkspacedata └── project.pbxproj └── README.md /GradientButton/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /GradientButton/Assets.xcassets/right.imageset/矩形-4-拷贝-2@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WooNoah/GradientCategory/HEAD/GradientButton/Assets.xcassets/right.imageset/矩形-4-拷贝-2@2x.png -------------------------------------------------------------------------------- /GradientButton/Assets.xcassets/rightArrow.imageset/rightArrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WooNoah/GradientCategory/HEAD/GradientButton/Assets.xcassets/rightArrow.imageset/rightArrow.png -------------------------------------------------------------------------------- /GradientButton.xcodeproj/xcuserdata/tb.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /GradientButton/Assets.xcassets/rightArrow.imageset/rightArrow@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WooNoah/GradientCategory/HEAD/GradientButton/Assets.xcassets/rightArrow.imageset/rightArrow@2x.png -------------------------------------------------------------------------------- /GradientButton/Assets.xcassets/gradientButton.imageset/gradientButton@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WooNoah/GradientCategory/HEAD/GradientButton/Assets.xcassets/gradientButton.imageset/gradientButton@2x.png -------------------------------------------------------------------------------- /GradientButton.xcodeproj/project.xcworkspace/xcuserdata/tb.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WooNoah/GradientCategory/HEAD/GradientButton.xcodeproj/project.xcworkspace/xcuserdata/tb.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /GradientButton.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /GradientButton/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // testLayer 4 | // 5 | // Created by tb on 17/3/17. 6 | // Copyright © 2017年 com.tb. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /GradientButton/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // testLayer 4 | // 5 | // Created by tb on 17/3/17. 6 | // Copyright © 2017年 com.tb. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /GradientButton/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // testLayer 4 | // 5 | // Created by tb on 17/3/17. 6 | // Copyright © 2017年 com.tb. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /GradientButton/Assets.xcassets/right.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "矩形-4-拷贝-2@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /GradientButton/Assets.xcassets/gradientButton.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "gradientButton@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /GradientButton/Assets.xcassets/rightArrow.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "rightArrow.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "filename" : "rightArrow@2x.png", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "universal", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /GradientButton.xcodeproj/xcuserdata/tb.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | GradientButton.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 3396D1E91E7B6CD200DADBDA 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /GradientButton/UIButton+Gradient.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIButton+Gradient.h 3 | // testLayer 4 | // 5 | // Created by tb on 17/3/17. 6 | // Copyright © 2017年 com.tb. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #import "UIImage+Gradient.h" 12 | 13 | @interface UIButton (Gradient) 14 | 15 | /** 16 | * 根据给定的颜色,设置按钮的颜色 17 | * @param btnSize 这里要求手动设置下生成图片的大小,防止coder使用第三方layout,没有设置大小 18 | * @param clrs 渐变颜色的数组 19 | * @param percent 渐变颜色的占比数组 20 | * @param type 渐变色的类型 21 | */ 22 | - (UIButton *)gradientButtonWithSize:(CGSize)btnSize colorArray:(NSArray *)clrs percentageArray:(NSArray *)percent gradientType:(GradientType)type; 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /GradientButton/UIButton+Gradient.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIButton+Gradient.m 3 | // testLayer 4 | // 5 | // Created by tb on 17/3/17. 6 | // Copyright © 2017年 com.tb. All rights reserved. 7 | // 8 | 9 | #import "UIButton+Gradient.h" 10 | 11 | @implementation UIButton (Gradient) 12 | 13 | - (UIButton *)gradientButtonWithSize:(CGSize)btnSize colorArray:(NSArray *)clrs percentageArray:(NSArray *)percent gradientType:(GradientType)type { 14 | 15 | UIImage *backImage = [[UIImage alloc]createImageWithSize:btnSize gradientColors:clrs percentage:percent gradientType:type]; 16 | 17 | [self setBackgroundImage:backImage forState:UIControlStateNormal]; 18 | 19 | return self; 20 | } 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /GradientButton/UIImage+Gradient.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+Gradient.h 3 | // testLayer 4 | // 5 | // Created by tb on 17/3/17. 6 | // Copyright © 2017年 com.tb. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | typedef NS_ENUM(NSInteger, GradientType) { 12 | GradientFromTopToBottom = 1, //从上到下 13 | GradientFromLeftToRight, //从做到右 14 | GradientFromLeftTopToRightBottom, //从上到下 15 | GradientFromLeftBottomToRightTop //从上到下 16 | }; 17 | 18 | @interface UIImage (Gradient) 19 | 20 | /** 21 | * 根据给定的颜色,生成渐变色的图片 22 | * @param imageSize 要生成的图片的大小 23 | * @param colorArr 渐变颜色的数组 24 | * @param percents 渐变颜色的占比数组 25 | * @param gradientType 渐变色的类型 26 | */ 27 | - (UIImage *)createImageWithSize:(CGSize)imageSize gradientColors:(NSArray *)colorArr percentage:(NSArray *)percents gradientType:(GradientType)gradientType; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /GradientButton/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | } 43 | ], 44 | "info" : { 45 | "version" : 1, 46 | "author" : "xcode" 47 | } 48 | } -------------------------------------------------------------------------------- /GradientButton/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GradientCategory 2 | 使用category实现gradient 3 | 4 | #### 简介 5 | ***本例主要采用了类别来实现了给按钮设置渐变色的功能*** 6 | 当然,里边也有一些别的对比实现方法. 7 | 各位看官如有发现什么bug,请批评指正! 8 | 9 | #### 效果图 10 | ![](https://upload-images.jianshu.io/upload_images/1241385-66a2fa0a30010ca2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 11 | 12 | #### 来看`.h`文件 13 | ``` 14 | 15 | #import 16 | 17 | typedef NS_ENUM(NSInteger, GradientType) { 18 | GradientFromTopToBottom = 1, //从上到下 19 | GradientFromLeftToRight, //从做到右 20 | GradientFromLeftTopToRightBottom, //从上到下 21 | GradientFromLeftBottomToRightTop //从上到下 22 | }; 23 | 24 | @interface UIImage (Gradient) 25 | 26 | /** 27 | * 根据给定的颜色,生成渐变色的图片 28 | * @param imageSize 要生成的图片的大小 29 | * @param colorArr 渐变颜色的数组 30 | * @param percents 渐变颜色的占比数组 31 | * @param gradientType 渐变色的类型 32 | */ 33 | - (UIImage *)createImageWithSize:(CGSize)imageSize gradientColors:(NSArray *)colorArr percentage:(NSArray *)percents gradientType:(GradientType)gradientType; 34 | 35 | @end 36 | 37 | 38 | #import 39 | 40 | #import "UIImage+Gradient.h" 41 | 42 | @interface UIButton (Gradient) 43 | 44 | /** 45 | * 根据给定的颜色,设置按钮的颜色 46 | * @param btnSize 这里要求手动设置下生成图片的大小,防止coder使用第三方layout,没有设置大小 47 | * @param clrs 渐变颜色的数组 48 | * @param percent 渐变颜色的占比数组 49 | * @param type 渐变色的类型 50 | */ 51 | - (UIButton *)gradientButtonWithSize:(CGSize)btnSize colorArray:(NSArray *)clrs percentageArray:(NSArray *)percent gradientType:(GradientType)type; 52 | 53 | @end 54 | ``` 55 | 56 | #### 实现 57 | [请大家移步Github](https://github.com/WooNoah/GradientCategory) 58 | 如果感觉有帮到您,***麻烦给个star***,谢谢! 59 | -------------------------------------------------------------------------------- /GradientButton/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /GradientButton/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // testLayer 4 | // 5 | // Created by tb on 17/3/17. 6 | // Copyright © 2017年 com.tb. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /GradientButton/UIImage+Gradient.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+Gradient.m 3 | // testLayer 4 | // 5 | // Created by tb on 17/3/17. 6 | // Copyright © 2017年 com.tb. All rights reserved. 7 | // 8 | 9 | #import "UIImage+Gradient.h" 10 | 11 | @implementation UIImage (Gradient) 12 | 13 | - (UIImage *)createImageWithSize:(CGSize)imageSize gradientColors:(NSArray *)colors percentage:(NSArray *)percents gradientType:(GradientType)gradientType { 14 | 15 | NSAssert(percents.count <= 5, @"输入颜色数量过多,如果需求数量过大,请修改locations[]数组的个数"); 16 | 17 | NSMutableArray *ar = [NSMutableArray array]; 18 | for(UIColor *c in colors) { 19 | [ar addObject:(id)c.CGColor]; 20 | } 21 | 22 | // NSUInteger capacity = percents.count; 23 | // CGFloat locations[capacity]; 24 | CGFloat locations[5]; 25 | for (int i = 0; i < percents.count; i++) { 26 | locations[i] = [percents[i] floatValue]; 27 | } 28 | 29 | 30 | UIGraphicsBeginImageContextWithOptions(imageSize, YES, 1); 31 | CGContextRef context = UIGraphicsGetCurrentContext(); 32 | CGContextSaveGState(context); 33 | CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]); 34 | CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, locations); 35 | CGPoint start; 36 | CGPoint end; 37 | switch (gradientType) { 38 | case GradientFromTopToBottom: 39 | start = CGPointMake(imageSize.width/2, 0.0); 40 | end = CGPointMake(imageSize.width/2, imageSize.height); 41 | break; 42 | case GradientFromLeftToRight: 43 | start = CGPointMake(0.0, imageSize.height/2); 44 | end = CGPointMake(imageSize.width, imageSize.height/2); 45 | break; 46 | case GradientFromLeftTopToRightBottom: 47 | start = CGPointMake(0.0, 0.0); 48 | end = CGPointMake(imageSize.width, imageSize.height); 49 | break; 50 | case GradientFromLeftBottomToRightTop: 51 | start = CGPointMake(0.0, imageSize.height); 52 | end = CGPointMake(imageSize.width, 0.0); 53 | break; 54 | default: 55 | break; 56 | } 57 | CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); 58 | UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 59 | CGGradientRelease(gradient); 60 | CGContextRestoreGState(context); 61 | CGColorSpaceRelease(colorSpace); 62 | UIGraphicsEndImageContext(); 63 | return image; 64 | } 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /GradientButton/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // testLayer 4 | // 5 | // Created by tb on 17/3/17. 6 | // Copyright © 2017年 com.tb. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | #import "UIButton+Gradient.h" 12 | 13 | #define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1] 14 | 15 | @interface ViewController () 16 | @property (weak, nonatomic) IBOutlet UIButton *testButton; 17 | 18 | @property (weak, nonatomic) IBOutlet UIButton *testButton2; 19 | @property (weak, nonatomic) IBOutlet UIButton *button3; 20 | @end 21 | 22 | @implementation ViewController 23 | 24 | - (void)viewDidLoad { 25 | [super viewDidLoad]; 26 | // Do any additional setup after loading the view, typically from a nib. 27 | 28 | [self.testButton setImage:[UIImage imageNamed:@"rightArrow"] forState:UIControlStateNormal]; 29 | [self.testButton setBackgroundImage:[UIImage imageNamed:@"gradientButton"] forState:UIControlStateNormal]; 30 | 31 | [self.testButton2 setImage:[UIImage imageNamed:@"right"] forState:UIControlStateNormal]; 32 | [self.testButton2 setBackgroundImage:[UIImage imageNamed:@"gradientButton"] forState:UIControlStateNormal]; 33 | [self.testButton2 gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)RGB(55, 188, 253),(id)RGB(0, 0, 0)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom]; 34 | 35 | 36 | 37 | 38 | UIButton *thirdBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 39 | thirdBtn.frame = CGRectMake(10, 50, 200, 44); 40 | 41 | CAGradientLayer *gradientLayer = [CAGradientLayer layer]; 42 | gradientLayer.frame = CGRectMake(0, 0, 200, 44); 43 | gradientLayer.startPoint = CGPointMake(0, 0); 44 | gradientLayer.endPoint = CGPointMake(1, 0); 45 | gradientLayer.locations = @[@(0.1),@(1.0)]; 46 | [gradientLayer setColors:@[(id)[RGB(46, 229, 253) CGColor],(id)[RGB(41, 195, 252) CGColor]]]; 47 | [thirdBtn.layer addSublayer:gradientLayer]; 48 | 49 | 50 | [thirdBtn setTitle:@"代码创建的按钮,使用layer" forState:UIControlStateNormal]; 51 | [thirdBtn setImage:[UIImage imageNamed:@"right"] forState:UIControlStateNormal]; 52 | [self.view addSubview:thirdBtn]; 53 | 54 | 55 | UIButton *four = [UIButton buttonWithType:UIButtonTypeCustom]; 56 | four.frame = CGRectMake(20, 120, 250, 44); 57 | [four setTitle:@"代码创建的按钮,使用Category" forState:UIControlStateNormal]; 58 | [four setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 59 | [four setImage:[UIImage imageNamed:@"right"] forState:UIControlStateNormal]; 60 | [four gradientButtonWithSize:CGSizeMake(200, 44) colorArray:@[(id)[UIColor yellowColor],(id)[UIColor brownColor]] percentageArray:@[@(0.18),@(1)] gradientType:GradientFromLeftBottomToRightTop]; 61 | [self.view addSubview:four]; 62 | 63 | 64 | [self.button3 gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)RGB(253, 175, 55),(id)RGB(91, 7, 7)] percentageArray:@[@(0.3),@(1)] gradientType:GradientFromTopToBottom]; 65 | 66 | } 67 | 68 | 69 | 70 | - (void)didReceiveMemoryWarning { 71 | [super didReceiveMemoryWarning]; 72 | // Dispose of any resources that can be recreated. 73 | } 74 | 75 | 76 | @end 77 | -------------------------------------------------------------------------------- /GradientButton.xcodeproj/xcuserdata/tb.xcuserdatad/xcschemes/GradientButton.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /GradientButton/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 32 | 36 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /GradientButton.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 33192A3F1E7BDD6900BCFF26 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 33192A311E7BDD6900BCFF26 /* AppDelegate.m */; }; 11 | 33192A401E7BDD6900BCFF26 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 33192A321E7BDD6900BCFF26 /* Assets.xcassets */; }; 12 | 33192A411E7BDD6900BCFF26 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 33192A331E7BDD6900BCFF26 /* LaunchScreen.storyboard */; }; 13 | 33192A421E7BDD6900BCFF26 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 33192A351E7BDD6900BCFF26 /* Main.storyboard */; }; 14 | 33192A431E7BDD6900BCFF26 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 33192A371E7BDD6900BCFF26 /* Info.plist */; }; 15 | 33192A441E7BDD6900BCFF26 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 33192A381E7BDD6900BCFF26 /* main.m */; }; 16 | 33192A451E7BDD6900BCFF26 /* UIButton+Gradient.m in Sources */ = {isa = PBXBuildFile; fileRef = 33192A3A1E7BDD6900BCFF26 /* UIButton+Gradient.m */; }; 17 | 33192A461E7BDD6900BCFF26 /* UIImage+Gradient.m in Sources */ = {isa = PBXBuildFile; fileRef = 33192A3C1E7BDD6900BCFF26 /* UIImage+Gradient.m */; }; 18 | 33192A471E7BDD6900BCFF26 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 33192A3E1E7BDD6900BCFF26 /* ViewController.m */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXFileReference section */ 22 | 33192A301E7BDD6900BCFF26 /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 23 | 33192A311E7BDD6900BCFF26 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 24 | 33192A321E7BDD6900BCFF26 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 25 | 33192A341E7BDD6900BCFF26 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 26 | 33192A361E7BDD6900BCFF26 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 27 | 33192A371E7BDD6900BCFF26 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 28 | 33192A381E7BDD6900BCFF26 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 29 | 33192A391E7BDD6900BCFF26 /* UIButton+Gradient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIButton+Gradient.h"; sourceTree = ""; }; 30 | 33192A3A1E7BDD6900BCFF26 /* UIButton+Gradient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIButton+Gradient.m"; sourceTree = ""; }; 31 | 33192A3B1E7BDD6900BCFF26 /* UIImage+Gradient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+Gradient.h"; sourceTree = ""; }; 32 | 33192A3C1E7BDD6900BCFF26 /* UIImage+Gradient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+Gradient.m"; sourceTree = ""; }; 33 | 33192A3D1E7BDD6900BCFF26 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 34 | 33192A3E1E7BDD6900BCFF26 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 35 | 3396D1EA1E7B6CD200DADBDA /* GradientButton.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GradientButton.app; sourceTree = BUILT_PRODUCTS_DIR; }; 36 | /* End PBXFileReference section */ 37 | 38 | /* Begin PBXFrameworksBuildPhase section */ 39 | 3396D1E71E7B6CD200DADBDA /* Frameworks */ = { 40 | isa = PBXFrameworksBuildPhase; 41 | buildActionMask = 2147483647; 42 | files = ( 43 | ); 44 | runOnlyForDeploymentPostprocessing = 0; 45 | }; 46 | /* End PBXFrameworksBuildPhase section */ 47 | 48 | /* Begin PBXGroup section */ 49 | 33192A2F1E7BDD6900BCFF26 /* GradientButton */ = { 50 | isa = PBXGroup; 51 | children = ( 52 | 33192A301E7BDD6900BCFF26 /* AppDelegate.h */, 53 | 33192A311E7BDD6900BCFF26 /* AppDelegate.m */, 54 | 33192A321E7BDD6900BCFF26 /* Assets.xcassets */, 55 | 33192A331E7BDD6900BCFF26 /* LaunchScreen.storyboard */, 56 | 33192A351E7BDD6900BCFF26 /* Main.storyboard */, 57 | 33192A371E7BDD6900BCFF26 /* Info.plist */, 58 | 33192A381E7BDD6900BCFF26 /* main.m */, 59 | 33192A391E7BDD6900BCFF26 /* UIButton+Gradient.h */, 60 | 33192A3A1E7BDD6900BCFF26 /* UIButton+Gradient.m */, 61 | 33192A3B1E7BDD6900BCFF26 /* UIImage+Gradient.h */, 62 | 33192A3C1E7BDD6900BCFF26 /* UIImage+Gradient.m */, 63 | 33192A3D1E7BDD6900BCFF26 /* ViewController.h */, 64 | 33192A3E1E7BDD6900BCFF26 /* ViewController.m */, 65 | ); 66 | path = GradientButton; 67 | sourceTree = ""; 68 | }; 69 | 3396D1E11E7B6CD200DADBDA = { 70 | isa = PBXGroup; 71 | children = ( 72 | 33192A2F1E7BDD6900BCFF26 /* GradientButton */, 73 | 3396D1EB1E7B6CD200DADBDA /* Products */, 74 | ); 75 | sourceTree = ""; 76 | }; 77 | 3396D1EB1E7B6CD200DADBDA /* Products */ = { 78 | isa = PBXGroup; 79 | children = ( 80 | 3396D1EA1E7B6CD200DADBDA /* GradientButton.app */, 81 | ); 82 | name = Products; 83 | sourceTree = ""; 84 | }; 85 | /* End PBXGroup section */ 86 | 87 | /* Begin PBXNativeTarget section */ 88 | 3396D1E91E7B6CD200DADBDA /* GradientButton */ = { 89 | isa = PBXNativeTarget; 90 | buildConfigurationList = 3396D2011E7B6CD200DADBDA /* Build configuration list for PBXNativeTarget "GradientButton" */; 91 | buildPhases = ( 92 | 3396D1E61E7B6CD200DADBDA /* Sources */, 93 | 3396D1E71E7B6CD200DADBDA /* Frameworks */, 94 | 3396D1E81E7B6CD200DADBDA /* Resources */, 95 | ); 96 | buildRules = ( 97 | ); 98 | dependencies = ( 99 | ); 100 | name = GradientButton; 101 | productName = testLayer; 102 | productReference = 3396D1EA1E7B6CD200DADBDA /* GradientButton.app */; 103 | productType = "com.apple.product-type.application"; 104 | }; 105 | /* End PBXNativeTarget section */ 106 | 107 | /* Begin PBXProject section */ 108 | 3396D1E21E7B6CD200DADBDA /* Project object */ = { 109 | isa = PBXProject; 110 | attributes = { 111 | LastUpgradeCheck = 0820; 112 | ORGANIZATIONNAME = com.tb; 113 | TargetAttributes = { 114 | 3396D1E91E7B6CD200DADBDA = { 115 | CreatedOnToolsVersion = 8.2.1; 116 | ProvisioningStyle = Automatic; 117 | }; 118 | }; 119 | }; 120 | buildConfigurationList = 3396D1E51E7B6CD200DADBDA /* Build configuration list for PBXProject "GradientButton" */; 121 | compatibilityVersion = "Xcode 3.2"; 122 | developmentRegion = English; 123 | hasScannedForEncodings = 0; 124 | knownRegions = ( 125 | en, 126 | Base, 127 | ); 128 | mainGroup = 3396D1E11E7B6CD200DADBDA; 129 | productRefGroup = 3396D1EB1E7B6CD200DADBDA /* Products */; 130 | projectDirPath = ""; 131 | projectRoot = ""; 132 | targets = ( 133 | 3396D1E91E7B6CD200DADBDA /* GradientButton */, 134 | ); 135 | }; 136 | /* End PBXProject section */ 137 | 138 | /* Begin PBXResourcesBuildPhase section */ 139 | 3396D1E81E7B6CD200DADBDA /* Resources */ = { 140 | isa = PBXResourcesBuildPhase; 141 | buildActionMask = 2147483647; 142 | files = ( 143 | 33192A431E7BDD6900BCFF26 /* Info.plist in Resources */, 144 | 33192A421E7BDD6900BCFF26 /* Main.storyboard in Resources */, 145 | 33192A401E7BDD6900BCFF26 /* Assets.xcassets in Resources */, 146 | 33192A411E7BDD6900BCFF26 /* LaunchScreen.storyboard in Resources */, 147 | ); 148 | runOnlyForDeploymentPostprocessing = 0; 149 | }; 150 | /* End PBXResourcesBuildPhase section */ 151 | 152 | /* Begin PBXSourcesBuildPhase section */ 153 | 3396D1E61E7B6CD200DADBDA /* Sources */ = { 154 | isa = PBXSourcesBuildPhase; 155 | buildActionMask = 2147483647; 156 | files = ( 157 | 33192A451E7BDD6900BCFF26 /* UIButton+Gradient.m in Sources */, 158 | 33192A441E7BDD6900BCFF26 /* main.m in Sources */, 159 | 33192A3F1E7BDD6900BCFF26 /* AppDelegate.m in Sources */, 160 | 33192A461E7BDD6900BCFF26 /* UIImage+Gradient.m in Sources */, 161 | 33192A471E7BDD6900BCFF26 /* ViewController.m in Sources */, 162 | ); 163 | runOnlyForDeploymentPostprocessing = 0; 164 | }; 165 | /* End PBXSourcesBuildPhase section */ 166 | 167 | /* Begin PBXVariantGroup section */ 168 | 33192A331E7BDD6900BCFF26 /* LaunchScreen.storyboard */ = { 169 | isa = PBXVariantGroup; 170 | children = ( 171 | 33192A341E7BDD6900BCFF26 /* Base */, 172 | ); 173 | name = LaunchScreen.storyboard; 174 | sourceTree = ""; 175 | }; 176 | 33192A351E7BDD6900BCFF26 /* Main.storyboard */ = { 177 | isa = PBXVariantGroup; 178 | children = ( 179 | 33192A361E7BDD6900BCFF26 /* Base */, 180 | ); 181 | name = Main.storyboard; 182 | sourceTree = ""; 183 | }; 184 | /* End PBXVariantGroup section */ 185 | 186 | /* Begin XCBuildConfiguration section */ 187 | 3396D1FF1E7B6CD200DADBDA /* Debug */ = { 188 | isa = XCBuildConfiguration; 189 | buildSettings = { 190 | ALWAYS_SEARCH_USER_PATHS = NO; 191 | CLANG_ANALYZER_NONNULL = YES; 192 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 193 | CLANG_CXX_LIBRARY = "libc++"; 194 | CLANG_ENABLE_MODULES = YES; 195 | CLANG_ENABLE_OBJC_ARC = YES; 196 | CLANG_WARN_BOOL_CONVERSION = YES; 197 | CLANG_WARN_CONSTANT_CONVERSION = YES; 198 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 199 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 200 | CLANG_WARN_EMPTY_BODY = YES; 201 | CLANG_WARN_ENUM_CONVERSION = YES; 202 | CLANG_WARN_INFINITE_RECURSION = YES; 203 | CLANG_WARN_INT_CONVERSION = YES; 204 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 205 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 206 | CLANG_WARN_UNREACHABLE_CODE = YES; 207 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 208 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 209 | COPY_PHASE_STRIP = NO; 210 | DEBUG_INFORMATION_FORMAT = dwarf; 211 | ENABLE_STRICT_OBJC_MSGSEND = YES; 212 | ENABLE_TESTABILITY = YES; 213 | GCC_C_LANGUAGE_STANDARD = gnu99; 214 | GCC_DYNAMIC_NO_PIC = NO; 215 | GCC_NO_COMMON_BLOCKS = YES; 216 | GCC_OPTIMIZATION_LEVEL = 0; 217 | GCC_PREPROCESSOR_DEFINITIONS = ( 218 | "DEBUG=1", 219 | "$(inherited)", 220 | ); 221 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 222 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 223 | GCC_WARN_UNDECLARED_SELECTOR = YES; 224 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 225 | GCC_WARN_UNUSED_FUNCTION = YES; 226 | GCC_WARN_UNUSED_VARIABLE = YES; 227 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 228 | MTL_ENABLE_DEBUG_INFO = YES; 229 | ONLY_ACTIVE_ARCH = YES; 230 | SDKROOT = iphoneos; 231 | }; 232 | name = Debug; 233 | }; 234 | 3396D2001E7B6CD200DADBDA /* Release */ = { 235 | isa = XCBuildConfiguration; 236 | buildSettings = { 237 | ALWAYS_SEARCH_USER_PATHS = NO; 238 | CLANG_ANALYZER_NONNULL = YES; 239 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 240 | CLANG_CXX_LIBRARY = "libc++"; 241 | CLANG_ENABLE_MODULES = YES; 242 | CLANG_ENABLE_OBJC_ARC = YES; 243 | CLANG_WARN_BOOL_CONVERSION = YES; 244 | CLANG_WARN_CONSTANT_CONVERSION = YES; 245 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 246 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 247 | CLANG_WARN_EMPTY_BODY = YES; 248 | CLANG_WARN_ENUM_CONVERSION = YES; 249 | CLANG_WARN_INFINITE_RECURSION = YES; 250 | CLANG_WARN_INT_CONVERSION = YES; 251 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 252 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 253 | CLANG_WARN_UNREACHABLE_CODE = YES; 254 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 255 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 256 | COPY_PHASE_STRIP = NO; 257 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 258 | ENABLE_NS_ASSERTIONS = NO; 259 | ENABLE_STRICT_OBJC_MSGSEND = YES; 260 | GCC_C_LANGUAGE_STANDARD = gnu99; 261 | GCC_NO_COMMON_BLOCKS = YES; 262 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 263 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 264 | GCC_WARN_UNDECLARED_SELECTOR = YES; 265 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 266 | GCC_WARN_UNUSED_FUNCTION = YES; 267 | GCC_WARN_UNUSED_VARIABLE = YES; 268 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 269 | MTL_ENABLE_DEBUG_INFO = NO; 270 | SDKROOT = iphoneos; 271 | VALIDATE_PRODUCT = YES; 272 | }; 273 | name = Release; 274 | }; 275 | 3396D2021E7B6CD200DADBDA /* Debug */ = { 276 | isa = XCBuildConfiguration; 277 | buildSettings = { 278 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 279 | INFOPLIST_FILE = "$(SRCROOT)/GradientButton/Info.plist"; 280 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 281 | PRODUCT_BUNDLE_IDENTIFIER = com.tb.testGradientButton; 282 | PRODUCT_NAME = "$(TARGET_NAME)"; 283 | }; 284 | name = Debug; 285 | }; 286 | 3396D2031E7B6CD200DADBDA /* Release */ = { 287 | isa = XCBuildConfiguration; 288 | buildSettings = { 289 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 290 | INFOPLIST_FILE = "$(SRCROOT)/GradientButton/Info.plist"; 291 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 292 | PRODUCT_BUNDLE_IDENTIFIER = com.tb.testGradientButton; 293 | PRODUCT_NAME = "$(TARGET_NAME)"; 294 | }; 295 | name = Release; 296 | }; 297 | /* End XCBuildConfiguration section */ 298 | 299 | /* Begin XCConfigurationList section */ 300 | 3396D1E51E7B6CD200DADBDA /* Build configuration list for PBXProject "GradientButton" */ = { 301 | isa = XCConfigurationList; 302 | buildConfigurations = ( 303 | 3396D1FF1E7B6CD200DADBDA /* Debug */, 304 | 3396D2001E7B6CD200DADBDA /* Release */, 305 | ); 306 | defaultConfigurationIsVisible = 0; 307 | defaultConfigurationName = Release; 308 | }; 309 | 3396D2011E7B6CD200DADBDA /* Build configuration list for PBXNativeTarget "GradientButton" */ = { 310 | isa = XCConfigurationList; 311 | buildConfigurations = ( 312 | 3396D2021E7B6CD200DADBDA /* Debug */, 313 | 3396D2031E7B6CD200DADBDA /* Release */, 314 | ); 315 | defaultConfigurationIsVisible = 0; 316 | defaultConfigurationName = Release; 317 | }; 318 | /* End XCConfigurationList section */ 319 | }; 320 | rootObject = 3396D1E21E7B6CD200DADBDA /* Project object */; 321 | } 322 | --------------------------------------------------------------------------------