CAGradientLayer渐变色的使用

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    CAGradientLayer layer = [CAGradientLayer new];
    layer.frame = CGRectMake(20, CGRectGetHeight(self.view.frame)/2, CGRectGetWidth(self.view.frame)-20
    2, 10);
    layer.colors = @[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor greenColor].CGColor];
    //一下两个属性是核心点,(0,0)是左下角的点,(1,1)是右上角的点
    layer.startPoint = CGPointMake(0, 0);
    layer.endPoint = CGPointMake(1, 0);
    //可以理解成X从0渐变到1,Y不渐变
    //同样,既然Y不渐变,所以它等同于
    //layer.startPoint = CGPointMake(0, 1);
    // layer.endPoint = CGPointMake(1, 1);
    //换句话说,所谓的渐变就必须要有一个直差
    [self.view.layer addSublayer:layer];
    }

效果图

你可能感兴趣的:(CAGradientLayer渐变色的使用)