给视图添加渐变色,示例如下:
rect.origin.y = CGRectGetMaxY(headView.frame);
rect.size.height = 50;
UIView *titleView = [[UIView alloc] initWithFrame:rect];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = titleView.bounds;
gradient.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithRed:0 green:153/255.0 blue:243/255.0 alpha:1.0].CGColor,
(id)[UIColor colorWithRed:35/255.0 green:209/255.0 blue:249/255.0 alpha:1.0].CGColor,nil];
// 颜色的起点位置,递增,并且数量跟颜色数量相等
gradient.locations = @[@0.0, @1];
gradient.startPoint = CGPointMake(0, 0);
gradient.endPoint = CGPointMake(1, 0);
[titleView.layer addSublayer:gradient];
[self.contentView addSubview:titleView];
效果如下:
