iOS实现渐变背景色的三种方法
来源:互联网 发布:mysql gtid 1236 编辑:程序博客网 时间:2024/05/21 07:51
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:
@property (nonatomic) CGPoint inputPoint0; @property (nonatomic) CGPoint inputPoint1; @property (nonatomic) UIColor *inputColor0; @property (nonatomic) UIColor *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.
CAGradientLayer *layer = [CAGradientLayer new]; layer.colors = @[(__bridge id)_inputColor0.CGColor, (__bridge id)_inputColor1.CGColor]; layer.startPoint = _inputPoint0; layer.endPoint = _inputPoint1; layer.frame = self.bounds; [self.layeraddSublayer:layer];
2. CGGradientRef
About Core Graphics and Core Image, please refer here, a very very good article:
http://www.techotopia.com/index.php/An_iOS_7_Graphics_Tutorial_using_Core_Graphics_and_Core_Image
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:.
CGContextRef context = UIGraphicsGetCurrentContext(); UIGraphicsPushContext(context); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGFloat locations[] = {0,1}; NSArray *colors = @[(__bridge id)_inputColor0.CGColor, (__bridge id)_inputColor1.CGColor]; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef) colors, locations); CGColorSpaceRelease(colorSpace); CGPoint startPoint = (CGPoint){rect.size.width * _inputPoint0.x, rect.size.height * _inputPoint0.y}; CGPoint endPoint = (CGPoint){rect.size.width * _inputPoint1.x, rect.size.height * _inputPoint1.y}; CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); CGGradientRelease(gradient); UIGraphicsPopContext();
3. Core Image
The following code is called in drawRect:.
CIFilter *ciFilter = [CIFilter filterWithName:@"CILinearGradient"]; CIVector *vector0 = [CIVector vectorWithX:rect.size.width * _inputPoint0.x Y:rect.size.height * (1 - _inputPoint0.y)]; CIVector *vector1 = [CIVector vectorWithX:rect.size.width * _inputPoint1.x Y:rect.size.height * (1 - _inputPoint1.y)]; [ciFilter setValue:vector0 forKey:@"inputPoint0"]; [ciFilter setValue:vector1 forKey:@"inputPoint1"]; [ciFilter setValue:[CIColor colorWithCGColor:_inputColor0.CGColor] forKey:@"inputColor0"]; [ciFilter setValue:[CIColor colorWithCGColor:_inputColor1.CGColor] forKey:@"inputColor1"]; CIImage *ciImage = ciFilter.outputImage; CIContext *con = [CIContext contextWithOptions:nil]; CGImageRef resultCGImage = [con createCGImage:ciImage fromRect:rect]; UIImage *resultUIImage = [UIImage imageWithCGImage:resultCGImage]; CGImageRelease(resultCGImage); [resultUIImage drawInRect:rect];
Sample code can be found here: https://github.com/RungeZhai/LGGradientBackgroundView
3 0
- iOS实现渐变背景色的三种方法
- iOS实现渐变背景色的三种方法
- IOS实现背景色渐变
- IOS 绘制背景色渐变的矩形
- IOS 绘制背景色渐变的矩形
- IOS 绘制背景色渐变的矩形
- iOS UIView背景色渐变
- css3背景色渐变方法
- 设置渐变的背景色
- 不断渐变的背景色
- Ogre-渐变背景色(gradient background)的实现
- Ogre-渐变背景色(gradient background)的实现
- Ogre-渐变背景色(gradient background)的实现
- css样式实现 背景色的渐变效果及兼容性
- android 实现View的背景色渐变动画
- 按钮实现鼠标悬停背景色渐变的动画特效
- iOS代码设置渐变背景色
- CSS实现表格背景色渐变色
- Java+MyEclipse快捷方式集锦
- 关于SEL的简单总结
- PHP获取时间的一些常用方法
- H5 性能调优 工具
- mysql 导出excel
- iOS实现渐变背景色的三种方法
- 三维激光扫描技术简介
- java.util.Date、java.sql.Date、java.sql.Time、java.sql.Timestamp区别和总结
- keytool 错误 java.io.IOException: incorrect keystore format
- leetcode: Combination Sum III
- Java实现网络爬虫
- 拉格朗日中值定理 简明版
- Robotium Instrumentation run failed due to 'java.lang.ClassNotFoundException'
- fatal error LNK1140临时解决方案