渐变色

83 阅读1分钟
#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) UIView *baseView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.baseView];
}

- (UIView *)baseView {
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0,100, [UIScreen mainScreen].bounds.size.width, 100)];
        UIColor *colorOne = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
        UIColor *colorTwo = [UIColor colorWithRed:1/255.0 green:120/255.0 blue:110/255.0 alpha:.5];
        NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.startPoint = CGPointMake(0, 1);//设置开始和结束位置(通过开始和结束位置来控制渐变的方向)
        gradient.endPoint = CGPointMake(0, 0.5);
        gradient.colors = colors;
        gradient.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100);
        [_baseView.layer insertSublayer:gradient atIndex:0];
    }
    return _baseView;
}

@end