视图背景颜色渐变的两种实现方式

来源:互联网 发布:大学生网络安全意识 编辑:程序博客网 时间:2024/05/29 16:53
  • 使用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;}

简书地址

原创粉丝点击