渐变色
1 - (void)setupDurationLabelBgView 2 { 3 CGFloat bgimageViewWidth = 165 * kBigScreenViewWidthRate; 4 CGFloat bgimageViewHeight = 20 * kBigScreenViewWidthRate; 5 6 UIView *durationLabelBgView = [[UIView alloc] init]; 7 [_imgView addSubview:durationLabelBgView]; 8 [durationLabelBgView mas_makeConstraints:^(MASConstraintMaker *make) { 9 make.left.right.bottom.mas_equalTo(_imgView); 10 make.height.mas_equalTo(bgimageViewHeight); 11 }]; 12 UIColor *color1 = UIColorFromHexWithAlpha(0x000000, 0); 13 UIColor *color2 = UIColorFromHexWithAlpha(0x000000, 1); 14 CAGradientLayer *commonLayer = [self gradientLayerWithColor1:color1 color2:color2]; 15 commonLayer.frame = CGRectMake(0, 0, bgimageViewWidth, bgimageViewHeight); 16 [durationLabelBgView.layer addSublayer:commonLayer]; 17 } 18 19 - (CAGradientLayer *)gradientLayerWithColor1:(UIColor *)color1 color2:(UIColor *)color2{ 20 //初始化CAGradientlayer对象,使它的大小为UIView的大小 21 CAGradientLayer *gradientLayer = [CAGradientLayer layer]; 22 23 //设置渐变区域的起始和终止位置(范围为0-1) 24 gradientLayer.startPoint = CGPointMake(0, 0); 25 gradientLayer.endPoint = CGPointMake(1, 0); 26 27 gradientLayer.colors = @[(__bridge id)color1.CGColor, 28 (__bridge id)color2.CGColor]; 29 //设置颜色分割点(范围:0-1) 30 gradientLayer.locations = @[@(0.0f), @(0.6)]; 31 32 return gradientLayer; 33 }