iOS设置颜色渐变

pickerView是要设置渐变的控件

添加如下代码 

CAGradientLayer *_gradLayer = [CAGradientLayer layer];

    NSArray *colors = [NSArray arrayWithObjects:

                      (id)[[UIColor colorWithWhite:0.0f alpha:0.5f] CGColor],

                      (id)[[UIColor colorWithWhite:0.0f alpha:0.7f] CGColor],

                      (id)[[UIColor colorWithWhite:0.0f alpha:1.0f] CGColor],

                      (id)[[UIColor colorWithWhite:0.0f alpha:0.7f] CGColor],

                      (id)[[UIColor colorWithWhite:0.0f alpha:0.5f] CGColor],

                      nil];

    [_gradLayer setColors:colors];

    [_gradLayer setStartPoint:CGPointMake(0.0f, 1.0f)];

    [_gradLayer setEndPoint:CGPointMake(0.0f, 0.0f)];

    [_gradLayer setFrame:pickerView.bounds];

    [pickerView.layer addSublayer:_gradLayer];

你可能感兴趣的:(iOS设置颜色渐变)