Core Graphices 设置渐变

Quartz 提供了两种设置渐变的方式  CGShadingRef and CGGradientRef

尝试CGGradientRef 的使用

import <UIKit/UIKit.h>

@interface GradientDemoA : UIView

@property (nonatomic,copy,nonnull)NSArray *colorArray;
@property (nonatomic,copy,nonnull) NSArray *locations;
@property (nonatomic,assign)CGPoint startPoint;
@property (nonatomic,assign)CGPoint endPoint; - (void)showViewWithColorArray:(NSArray * _Nullable)colorArray GradientLocations:(NSArray * _Nullable)loc startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint;
@end
#import "GradientDemoA.h"

@implementation GradientDemoA

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
}
return self;
} - (void)showViewWithColorArray:(NSArray *)colorArray GradientLocations:( NSArray *)loc startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint{ self.colorArray = colorArray;
self.locations = loc;
self.startPoint = startPoint;
self.endPoint = endPoint;
[self setNeedsDisplay];
} - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient;
size_t num_location = self.colorArray.count; CGFloat components[self.colorArray.count*]; for (int i=; i<self.colorArray.count; i++) {
CGFloat *colorComponents = (CGFloat *)CGColorGetComponents(((UIColor *)self.colorArray[i]).CGColor);
components[+i*] = colorComponents[];
components[+i*] = colorComponents[];
components[+i*] = colorComponents[];
components[+i*] = colorComponents[]; } CGFloat gendientLocations[self.locations.count]; for (int j= ; j<self.locations.count; j++) { gendientLocations[j] = (CGFloat)([self.locations[j] doubleValue]);
} // 参数的意义
/* 颜色空间
components cgfloat 数组 RGB的颜色值
{
red1,green1,blue1,alpha1,
red2,green2,blue2,alpha2,
...
}
gendientLocations 颜色渐变的点 CGFloat gendientLocation[2] = {0.0,.......,1.0}; num_location 颜色渐变点的个数;
*/ gradient = CGGradientCreateWithColorComponents(colorSpace, components, gendientLocations, num_location); /*
开始点与结束点的tan值决定了渐变的角度;
**/ // CGContextDrawLinearGradient(context, gradient, self.startPoint, self.endPoint, kCGGradientDrawsBeforeStartLocation); CGContextDrawRadialGradient(context, gradient, CGPointMake(, ), , CGPointMake(, ), , kCGGradientDrawsAfterEndLocation); CGGradientRelease(gradient); } @end

参数更具体的意义请查看:

http://blog.csdn.net/u012890117/article/details/17606755

https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html#//apple_ref/doc/uid/TP30001066-CH207-TPXREF101

测试代码:
- (void)gradientColor{
GradientDemoA *my = [[GradientDemoA alloc] initWithFrame:CGRectMake(, , , )]; [my showViewWithColorArray:@[[[UIColor redColor] colorWithAlphaComponent:1.0],[[UIColor redColor] colorWithAlphaComponent:0.0],[[UIColor redColor] colorWithAlphaComponent:0.8]] GradientLocations: @[@(0.1),@0.5,@(0.8)] startPoint:CGPointMake(,) endPoint:CGPointMake(, )]; [self.view addSubview:my];
}

图1:

图2:

 

Core Graphices 设置渐变的更多相关文章

  1. Core Graphices 获取上下文

    Core Graphices 获取上下文的三种方式: 1.自定义view 重写view 的 drawRect:(CGRect)rect方法 - (void)drawRect:(CGRect)rect ...

  2. [转载]linux下core文件设置与查看

    转自:https://blog.csdn.net/dingqinghui/article/details/77855330?locationNum=9&fps=1 linux下core文件设置 ...

  3. canvas设置渐变

    canvas设置渐变 方法 createLinearGradient(x1, y1, x2, y2) 线性渐变 createRadialGradient(x1, y1, r1, x2, y2, r2) ...

  4. linux core文件设置

     http://blog.csdn.net/ctthuangcheng/article/details/8963551 linux core文件设置 分类: Linux OS Debugging Te ...

  5. CSS渐变色边框,解决border设置渐变后,border-radius无效的问题

    需求:用css设置渐变边框通过border-image来实现渐变色边框 <div class="content"></div> .content { wid ...

  6. label设置渐变时不显示纯英文纯数字字符串

    提出问题: 当对UILabel设置渐变color时,有点小问题.即:text为中文或中英混合字符串时显示正常,纯英文字符串不显示!!!   剖析问题: 经搜索了解到:在显示中文时,绘制渐变color的 ...

  7. Linux 下如何产生core文件(core dump设置)

    转自:https://blog.csdn.net/star_xiong/article/details/43529637 今天在Linux下调试C程序时,出现段错误,习惯性的ls下当前目录,发现没有生 ...

  8. ASP.NET Core如何设置请求超时时间

    如果一个请求在ASP.NET Core中运行太久,会导致请求超时,目前ASP.NET Core对请求超时的设置比较麻烦,本文列出目前收集到的一些方法,供大家参考. 部署ASP.NET Core到IIS ...

  9. ASP.NET Core 如何设置发布环境

    在ASP.NET Core中自带了一些内置对象,可以读取到当前程序处于什么样的环境当中,比如在ASP.NET Core的Startup类的Configure方法中,我们就会看到这么一段代码: publ ...

随机推荐

  1. vue 打开新页面

    <router-link tag="a" target="_blank" :to="{path:'/system/detail?id=' + s ...

  2. js中的setTimeout第三个参数

    setTimeout跟setInterval大家应该都很熟悉的,但是一直很少注意,原来这两个函数可以支持第三个参数的,但是IE就呵呵了,仅IE(6-9)呵呵了,其他浏览器都支持的。 第三个参数将作为回 ...

  3. mysql装完计算机管理里面没mysql服务怎么解决(转)

    mysql装完计算机管理里面没mysql服务怎么解决 2017年07月04日 09:32:51 XC_Echizen 阅读数:7335 标签: mysql计算机mysql服务找不到mysql服务 更多 ...

  4. 第十二次作业 - Beta答辩总结

    目录 前言 项目的链接与宣传 项目总结 原计划 达成情况 原因分析 [ Beta 冲刺博客链接汇总] [燃尽图] Beta版本展示 使用说明 视频展示 图片展示 答辩总结 [团队中个人的贡献比例] [ ...

  5. MySql5.7 Distinct与Order By同时使用报错的解决方案

    mysql5.7版本中,如果DISTINCT和order by一起使用将会报3065错误,sql语句无法执行.这是由于5.7版本语法比之前版本语法要求更加严格导致的. 解决方案: 1.vim /etc ...

  6. linux c 输出信息到console

    static void console_log(const char *format, ...) { static FILE *fpConsole; if (fpConsole == NULL) { ...

  7. web(七)css的语法规则、注释

    css的语法规则:特殊的css语法标识. !important:当使用多种方式设定标签样式时,设定样式渲染的应用优先权,声明在取值之后. .important { color: red !import ...

  8. Thrift-0.10.0 CenOS 7 编译错误 error: expected ')' before 'PRIu32'

    Thrift-0.10.0 CenOS 7 编译错误 error: expected ')' before 'PRIu32' 在编译Thrift的时候,无论是Apache官网tar包,还是Github ...

  9. License控制解决方案

    当我们写完一个软件以后一般都会牵扯到软件控制,那么控制版本的原理是什么呢?其实就是在程序中添加了一段经过自己编写算法(这个算法可以是简单的公式运算,也可以是复杂的结合硬件的绑定方式),将形成的序列号注 ...

  10. Redis之在Linux上安装和简单的使用

    我只是一个搬运工 Redis之在Linux上安装和简单的使用https://blog.csdn.net/qq_20989105/article/details/76390367 一.安装gcc 1.R ...