視圖背景顏色漸變的兩種實現方式


  • 使用layer方式實現

//初始化視圖
self.comfirmButton.frame = CGRectMake(139, 0, self.width - 139, self.height);
//初始化CAGradientlayer對象,使它的大小為對象的大小
CAGradientLayer *layer = [CAGradientLayer layer];
layer.frame = self.comfirmButton.bounds;
//將layer對象添加到視圖
[self.comfirmButton.layer addSublayer:layer];
//漸變區域的起始點
layer.startPoint = CGPointMake(0, 0);
//漸變區域的終點
layer.endPoint = CGPointMake(1, 0);
//顏色數組
layer.colors = @[(__bridge id)[UIColor colorWithHexString:@"FF7171"].CGColor,(__bridge id)[UIColor colorWithHexString:@"FF71A1"].CGColor];
//設置顏色的分割點
layer.locations = @[@(0.5f),@(1.0f)];


  • 使用上下文的方式實現

    • .h中的方法
    • 類目


      typedef enum {
      topToBottom = 0,//從上到小
      leftToRight = 1,//從左到右
      upleftTolowRight = 2,//左上到右下
      uprightTolowLeft = 3,//右上到左下
      }GradientType;
      + (UIButton *)changeButtonTitleAndImagePointWith:(UIButton *)button

外部方法

+ (UIButton *)changeButtonTitleAndImagePointWith:(UIButton *)button
{
CGFloat imageW = button.imageView.frame.size.width;
CGFloat imageH = button.imageView.frame.size.height;

CGFloat titleW = button.titleLabel.frame.size.width;
CGFloat titleH = button.titleLabel.frame.size.height;

//圖片上文字下
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -imageW, imageH + 10, 0.f)];
[button setImageEdgeInsets:UIEdgeInsetsMake(titleH, 0.f, 0.f,-titleW)];

return button;
}

私有方法

+ (UIButton *)changeColorArray:(NSMutableArray*)colorArray ByGradientType:(GradientType)gradientType button:(UIButton *)button
{
UIImage *backImage = [button buttonImageFromColors:colorArray ByGradientType:gradientType button:button];
[button setBackgroundImage:backImage forState:UIControlStateNormal];
return button;
}
- (UIImage*) buttonImageFromColors:(NSArray*)colors ByGradientType:(GradientType)gradientType button:(UIButton *)button{
NSMutableArray *ar = [NSMutableArray array];
for(UIColor *c in colors) {
[ar addObject:(id)c.CGColor];
}
UIGraphicsBeginImageContextWithOptions(button.frame.size, YES, 1);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
CGPoint start;
CGPoint end;

switch (gradientType) {
case 0:////從上到小
start = CGPointMake(0.0, 0.0);
end = CGPointMake(0.0, button.frame.size.height);
break;
case 1:////從左到右
start = CGPointMake(0.0, 0.0);
end = CGPointMake(button.frame.size.width, 0.0);
break;
case 2:////左上到右下
start = CGPointMake(0.0, 0.0);
end = CGPointMake(button.frame.size.width, button.frame.size.height);
break;
case 3: ////右上到左下
start = CGPointMake(button.frame.size.width, 0.0);
end = CGPointMake(0.0, button.frame.size.height);
break;
default:
break;
}
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGGradientRelease(gradient);
CGContextRestoreGState(context);
CGColorSpaceRelease(colorSpace);
UIGraphicsEndImageContext();
return image;
}

簡書地址


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com