iOS 按钮实现从左到右渐变色

iOS 按钮实现从左到右渐变色
原文链接:https://blog.csdn.net/gf771115/article/details/78571723

Simulator Screen Shot - iPhone X - 2018-09-01 at 10.50.43.png
#import "ViewController.h"
#define RGBA(r,g,b,a)       [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define MainColor1                 RGBA(255, 132, 23,  0.3)//主题橘色1
#define MainColor2                 RGBA(255, 132, 23,  1)//主题橘色2
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //渐变的按钮
    UIButton *lookBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    lookBtn.frame = CGRectMake(100, 100,100, 100);
    [lookBtn setTitle:@"查看详情" forState:(UIControlStateNormal)];
    //    [lookBtn addTarget:self action:@selector(lookBtn) forControlEvents:(UIControlEventTouchUpInside)];
    lookBtn.layer.shadowColor   = MainColor2.CGColor;
    lookBtn.layer.shadowOffset  = CGSizeMake(4, 4);
    lookBtn.layer.shadowOpacity = 0.5;
    lookBtn.layer.shadowRadius  = 5;
    
    //渐变颜色
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors     = @[(__bridge id)MainColor1.CGColor, (__bridge id)MainColor2.CGColor];
    gradientLayer.locations  = @[@0.3, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint   = CGPointMake(1.0, 0);
    gradientLayer.frame      = CGRectMake(0, 0,  100, 100);
    gradientLayer.cornerRadius = 50;
    [lookBtn.layer addSublayer:gradientLayer];
    [self.view addSubview:lookBtn];
}

你可能感兴趣的:(iOS 按钮实现从左到右渐变色)