iOS实现渐变背景色的三种方法

 In some particular circumstances, we want to use a gradient background view. As far as my knowledge can reach, there are three different ways to implement a gradient background: CAGradientLayer, CGGradient and CoreImage.
     First, Let’s have a look at the result.

     To define the gradient colour, we have at least four properties:
[objc]  view plain  copy
  1. @property (nonatomic) CGPoint inputPoint0;  
  2. @property (nonatomic) CGPoint inputPoint1;  
  3. @property (nonatomicUIColor *inputColor0;  
  4. @property (nonatomicUIColor *inputColor1;  

     The two CGPoint properties define where the gradient colour begins and ends. They are defined in a unit coordinate space where (0, 0) at the top left and (1, 1) at the bottom right. The two UIColor properties define the start colour and the end colour.

    1. CAGradientLayer
         We are not going to discuss the details of how to use CAGradientLayer. There is a good article talking about it:  http://www.cnblogs.com/YouXianMing/p/3793913.html.
[objc]  view plain  copy
  1. CAGradientLayer *layer = [CAGradientLayer new];  
  2. layer.colors = @[(__bridge id)_inputColor0.CGColor, (__bridge id)_inputColor1.CGColor];  
  3. layer.startPoint = _inputPoint0;  
  4. layer.endPoint = _inputPoint1;  
  5. layer.frame = self.bounds;             
  6. [self.layeraddSublayer:layer];  


    2. CGGradientRef
          About Core Graphics and Core Image, please refer here, a very very good article:
          Note that CAGradientLayer is using unit coordinate space while Core Graphics and Core Image are not. More Interestingly, Core Graphics’ coordinate space where (0, 0) starts at the top left is different from Core Image’s where (0, 0) starts at bottom left.
          The following code is called in drawRect:.
[objc]  view plain  copy
  1. CGContextRef context = UIGraphicsGetCurrentContext();  
  2. UIGraphicsPushContext(context);  
  3.   
  4. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  
  5. CGFloat locations[] = {0,1};  
  6. NSArray *colors = @[(__bridge id)_inputColor0.CGColor, (__bridge id)_inputColor1.CGColor];  
  7. CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef) colors, locations);  
  8. CGColorSpaceRelease(colorSpace);  
  9.   
  10. CGPoint startPoint = (CGPoint){rect.size.width * _inputPoint0.x, rect.size.height * _inputPoint0.y};  
  11. CGPoint endPoint = (CGPoint){rect.size.width * _inputPoint1.x, rect.size.height * _inputPoint1.y};  
  12. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);  
  13.     
  14. CGGradientRelease(gradient);  
  15. UIGraphicsPopContext();  


    3. Core Image
          The following code is called in drawRect:.
[objc]  view plain  copy
  1. CIFilter *ciFilter = [CIFilter filterWithName:@"CILinearGradient"];  
  2. CIVector *vector0 = [CIVector vectorWithX:rect.size.width * _inputPoint0.x Y:rect.size.height * (1 - _inputPoint0.y)];  
  3. CIVector *vector1 = [CIVector vectorWithX:rect.size.width * _inputPoint1.x Y:rect.size.height * (1 - _inputPoint1.y)];  
  4. [ciFilter setValue:vector0 forKey:@"inputPoint0"];  
  5. [ciFilter setValue:vector1 forKey:@"inputPoint1"];  
  6. [ciFilter setValue:[CIColor colorWithCGColor:_inputColor0.CGColor] forKey:@"inputColor0"];  
  7. [ciFilter setValue:[CIColor colorWithCGColor:_inputColor1.CGColor] forKey:@"inputColor1"];  
  8.    
  9. CIImage *ciImage = ciFilter.outputImage;  
  10. CIContext *con = [CIContext contextWithOptions:nil];  
  11. CGImageRef resultCGImage = [con createCGImage:ciImage  
  12.                                        fromRect:rect];  
  13. UIImage *resultUIImage = [UIImage imageWithCGImage:resultCGImage];  
  14. CGImageRelease(resultCGImage);  
  15.    
  16. [resultUIImage drawInRect:rect];  

 

     
     Sample code can be found here:  https://github.com/RungeZhai/LGGradientBackgroundView

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值