ios view 颜色变浅_iOS view 颜色渐变

//渐变色过渡自然

CAGradientLayer *layer = [CAGradientLayer layer];

layer.frame = CGRectMake(0, 0, curW-10,44);

layer.colors = [NSArray arrayWithObjects:(id)[UIColor clearColor].CGColor, (id)[UIColor grayColor].CGColor, nil];

for (CALayer *sublayer in [self.bgView.layer sublayers])

{

[sublayer removeFromSuperlayer];

}

[self.bgView.layer insertSublayer:layer atIndex:0];

//返回渐变的image

- (UIImage*) gradientImageFromColors:(NSArray*)colors ByGradientType:(GradientType)gradientType inSize:(CGSize)size{

NSMutableArray *ar = [NSMutableArray array];

for(UIColor *c in colors) {

[ar addObject:(id)c.CGColor];

}

UIGraphicsBeginImageContextWithOptions(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, size.height);

break;

case 1:

//左右渐变

start = CGPointMake(0.0, 0.0);

end = CGPointMake(size.width, 0.0);

break;

case 2:

//对角两侧渐变

start = CGPointMake(0.0, 0.0);

end = CGPointMake(size.width, size.height);

break;

case 3:

//对角两侧渐变

start = CGPointMake(size.width, 0.0);

end = CGPointMake(0.0, size.height);

break;

case 4:

//线性渐变

start = CGPointMake(size.width/2, size.height/2);

end = CGPointMake(size.width/2, size.height/2);

break;

default:

break;

}

CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);

if (gradientType == 4) {

CGContextDrawRadialGradient(context, gradient, start, 10, end, size.width/3, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);

}

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

CGGradientRelease(gradient);

CGContextRestoreGState(context);

CGColorSpaceRelease(colorSpace);

UIGraphicsEndImageContext();

return image;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值