iOS: CGPathRef上绘制渐变颜色
来源:互联网 发布:淘宝男鞋排行榜 编辑:程序博客网 时间:2024/05/16 15:29
转自:https://www.mgenware.com/blog/?p=2396
在iOS Core Graphics中绘制渐变颜色主要是通过使用CGContextDraw(xxx)Gradient
相关函数,比如,常见的线性渐变就是CGContextDrawLinearGradient
函数,这里帖子封装了绘制渐变颜色的具体代码过程,我们拿过来直接用,只不过原帖代码是用CGContextAddRect
画了一个矩形,我们改一下去画CGPathRef
。这里无论是什么形状其实无所谓,原理都是用来做Clip,所以需要在CGContextClip
函数前调用CGContextAddPath
函数把CGPathRef
加入到Context中。
另外一个需要注意的地方是渐变的方向,方向是由两个点控制的,点的单位就是坐标。因此需要正确从CGPathRef
中找到正确的点,方法当然有很多种看具体实现,本例中,我就是简单得通过调用CGPathGetBoundingBox
函数,返回CGPathRef
的矩形区域,然后根据这个矩形取两个点,读者可以根据自行需求修改具体代码。
OK,把代码封装成一个方法,类似这样:
/** * 修改自http://www.raywenderlich.com/32283/core-graphics-tutorial-lines-rectangles-and-gradients */- (void)drawLinearGradient:(CGContextRef)context path:(CGPathRef)path startColor:(CGColorRef)startColor endColor:(CGColorRef)endColor{ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGFloat locations[] = { 0.0, 1.0 }; NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor]; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations); CGRect pathRect = CGPathGetBoundingBox(path); //具体方向可根据需求修改 CGPoint startPoint = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMinY(pathRect)); CGPoint endPoint = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMaxY(pathRect)); CGContextSaveGState(context); CGContextAddPath(context, path); CGContextClip(context); CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); CGContextRestoreGState(context); CGGradientRelease(gradient); CGColorSpaceRelease(colorSpace);}
接下来就可以使用了,示例代码:
- (void)viewDidLoad { [super viewDidLoad]; //创建CGContextRef UIGraphicsBeginImageContext(self.view.bounds.size); CGContextRef gc = UIGraphicsGetCurrentContext(); //创建CGMutablePathRef CGMutablePathRef path = CGPathCreateMutable(); //绘制Path CGRect rect = CGRectInset(self.view.bounds, 30, 30); CGPathMoveToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect)); CGPathAddLineToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetHeight(rect)); CGPathAddLineToPoint(path, NULL, CGRectGetWidth(rect), CGRectGetHeight(rect) * 2 / 3); CGPathCloseSubpath(path); //绘制渐变 [self drawLinearGradient:gc path:path startColor:[UIColor greenColor].CGColor endColor:[UIColor redColor].CGColor]; //注意释放CGMutablePathRef CGPathRelease(path); //从Context中获取图像,并显示在界面上 UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; [self.view addSubview:imgView];}
效果:
1 0
- iOS: CGPathRef上绘制渐变颜色
- iOS绘制颜色渐变
- j2me绘制渐变颜色
- HTML5绘制颜色渐变
- 绘制颜色渐变三角形
- iOS开发--CGPathRef方法
- 用Bitmap绘制渐变颜色
- 使用Bitmap绘制渐变颜色
- VC 绘制颜色渐变区域
- PHP绘制渐变颜色图片
- 绘制颜色渐变矩形函数
- IOS 绘制 之渐变
- ios颜色渐变
- ios 颜色渐变效果
- IOS 颜色渐变
- 颜色渐变 ios
- ios颜色渐变
- iOS 颜色渐变
- leetcode微软面试题92Reverse Linked List II
- 奥维奥SAP B1Plus助力温州泵阀行业云转型
- [BZOJ1453][Wc]Dface双面棋盘(lct)
- mxnet模型保存和加载
- [前端]前端开发的历史和趋势
- iOS: CGPathRef上绘制渐变颜色
- fibonacci数列四种不同解法思路。
- 省选算法
- ORACLE服务监听器启动不了
- 02.HTTP路由
- Android实现验证码效果
- STS搭建SVN过程及同步项目(详细)
- 网易机试-工作安排
- 表达式求值