Core Graphices 设置渐变
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
测试代码:
- (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 设置渐变的更多相关文章
- Core Graphices 获取上下文
Core Graphices 获取上下文的三种方式: 1.自定义view 重写view 的 drawRect:(CGRect)rect方法 - (void)drawRect:(CGRect)rect ...
- [转载]linux下core文件设置与查看
转自:https://blog.csdn.net/dingqinghui/article/details/77855330?locationNum=9&fps=1 linux下core文件设置 ...
- canvas设置渐变
canvas设置渐变 方法 createLinearGradient(x1, y1, x2, y2) 线性渐变 createRadialGradient(x1, y1, r1, x2, y2, r2) ...
- linux core文件设置
http://blog.csdn.net/ctthuangcheng/article/details/8963551 linux core文件设置 分类: Linux OS Debugging Te ...
- CSS渐变色边框,解决border设置渐变后,border-radius无效的问题
需求:用css设置渐变边框通过border-image来实现渐变色边框 <div class="content"></div> .content { wid ...
- label设置渐变时不显示纯英文纯数字字符串
提出问题: 当对UILabel设置渐变color时,有点小问题.即:text为中文或中英混合字符串时显示正常,纯英文字符串不显示!!! 剖析问题: 经搜索了解到:在显示中文时,绘制渐变color的 ...
- Linux 下如何产生core文件(core dump设置)
转自:https://blog.csdn.net/star_xiong/article/details/43529637 今天在Linux下调试C程序时,出现段错误,习惯性的ls下当前目录,发现没有生 ...
- ASP.NET Core如何设置请求超时时间
如果一个请求在ASP.NET Core中运行太久,会导致请求超时,目前ASP.NET Core对请求超时的设置比较麻烦,本文列出目前收集到的一些方法,供大家参考. 部署ASP.NET Core到IIS ...
- ASP.NET Core 如何设置发布环境
在ASP.NET Core中自带了一些内置对象,可以读取到当前程序处于什么样的环境当中,比如在ASP.NET Core的Startup类的Configure方法中,我们就会看到这么一段代码: publ ...
随机推荐
- 单点登录系统实现基于SpringBoot
今天的干货有点湿,里面夹杂着我的泪水.可能也只有代码才能让我暂时的平静.通过本章内容你将学到单点登录系统和传统登录系统的区别,单点登录系统设计思路,Spring4 Java配置方式整合HttpClie ...
- vue轮播图实现
1.先安装组件 cnpm install vue-awesome-swiper; 2.在main.js下引入文件: import VueAwsomeSwiper from 'vue-awesome-s ...
- 告诉你们!我是怎么与Linux系统接触的!
最开始接触Linux是在15年来北京后,刚来北京机缘巧合,从事了实施工程师的工作.实施工作是一个面很广的工作.业务.技术.沟通等等方方面面的.技术一直是我是的短板.刚开始,公司在要在阿里云上部署APP ...
- mac 常用终端命令
mkdir 文件夹夹名称 创建一个文件夹 cd 文件夹名称 进入该文件夹 git init 在该文件夹下创建一个git仓库 touch 文件名称 在该文件夹下创建一个文件 echo '内容' &g ...
- 开始Flask项目
新建Flask项目. 设置调试模式. 理解Flask项目主程序. 使用装饰器,设置路径与函数之间的关系. 使用Flask中render_template,用不同的路径,返回首页.登录员.注册页. 用视 ...
- Centos7配置TiDB监控
一: 1.1:http://192.168.1.12:3000/ 1.2:点击install Grafana wget https://dl.grafana.com/oss/release/grafa ...
- timeline server介绍
1. timeline是什么 2. timeline能做什么 3. timeline结构 4. timeline怎么配置 1). timeline是什么? 它能存储和检索应用当前和历史信息在yar ...
- 磁性窗体设计C#(二)
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 1458 Common Subsequence
最简单的LIS; 设字符串为 a = acc b = cc 则dp数组为 0 0 1 1 1 2 b[0] = a[1], b[1] = a[1] 防止这里算两个 要清楚的是 怎么不重复计算 也就是 ...
- OperateResult 基础类及派生类介绍
前言 在Visual Studio 中的NuGet管理器中可以下载安装,也可以直接在NuGet控制台输入下面的指令安装: Install-Package HslCommunication NuGet安 ...