6 Specialzed layers 特殊层 第二部分 读书笔记
CAGradientLayer
CAGradientLayer is used to generate a smooth gradient between two or more colors. 是用来产生渐变色的。
It's possible to replicate the appearance of a CAGradientLayer using Core Graphics to draw into an ordinary layer's backing image, but the advantage of using a CAGradientLayer instead is that the drawing is hardware accelerated.
好处是硬件加速的。
Basic Gradients
We'll start with a simple diagonal gradient from red to blue (see Listing 6.6). The gradient colors are specified using the colors property, which is an array. The colors array expects values of type CGColorRef (which is not an NSObject derivative), so we need to use the bridging trick that we first saw in Chapter 2 to keep the compiler happy.
gradients colors 是一个数组,要用到CGColorRef 。
CAGradientLayer also has startPoint and endPoint properties that define the direction of the gradient. These are specified in unit coordinates, not points, so the top-left corner of the layer is specified with {0, 0} and the bottom-right corner is {1, 1}. The resulting gradient is shown in Figure 6.6.
unit coordinates,不是points,top left corner of the layer 是{0,0},而右下角是{1,1}
Listing 6.6 A Simple Two-Color Diagonal Gradient
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *containerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//create gradient layer and add it to our container view
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.containerView.bounds;
[self.containerView.layer addSublayer:gradientLayer];
//set gradient colors
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor blueColor].CGColor];
//set gradient start and end points
gradientLayer.startPoint = CGPointMake(0, 0); gradientLayer.endPoint = CGPointMake(1, 1);
}
@end

Multipart Gradients 多部分组成的Gradients
The colors array can contain as many colors as you like, so it is simple to create a multipart gradient such as a rainbow. By default, the colors in the gradient will be evenly spaced, but we can adjust the spacing using the locations property.
The locations property is an array of floating-point values (boxed as NSNumber objects). These values define the positions for each distinct color in the colors array, and are specified in unit coordinates, with 0.0 representing the start of the gradient and 1.0 representing the end.
locations 属性也是一个array .这些值定义了位置,为每个颜色,并指明在unit coordinates .
It is not obligatory to supply a locations array, but if you do, you must ensure that the number of locations matches the number of colors or you'll get a blank gradient.
指定locations 不是强制的,但是如果你做了,你需要确保locations 的数量和colors数量相同。否则,可能会获取一个空白的gradient
Listing 6.7 shows a modified version of the diagonal gradient code from Listing 6.6. We now have a three-part gradient from red to yellow to green. A locations array has been specified with the values 0.0, 0.25, and 0.5, which causes the gradient to be squashed up toward the top-left corner of the view (see Figure 6.7).
Listing 6.7 Using the locations Array to Offset a Gradient
- (void)viewDidLoad {
[super viewDidLoad];
//create gradient layer and add it to our container view
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.containerView.bounds;
[self.containerView.layer addSublayer:gradientLayer];
//set gradient colors
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor, (__bridge id)[UIColor greenColor].CGColor];
//set locations
gradientLayer.locations = @[@0.0, @0.25, @0.5];
//set gradient start and end points
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1, 1);
}

Figure 6.7 A three-color gradient, offset to the top left using the locations array
CAReplicatorLayer
The CAReplicatorLayer class is designed to efficiently generate collections of similar layers.
是为了产生相似layers的容器的。
It works by drawing one or more duplicate copies of each of its sublayers, applying a different transform to each duplicate. This is probably easier to demonstrate than to explain, so let's construct an example.
Repeating Layers
In Listing 6.8, we create a small white square layer in the middle of the screen, then turn it into a ring of ten layers using CAReplicatorLayer. The instanceCount property specifies how many times the layer should be repeated.
instanceCount 属性指明了这个layer需要复制多少次
The instanceTransform applies a CATransform3D (in this case, a translation and rotation that moves the layer to the next point in the circle).
The transform is applied incrementally, with each instance positioned relative to the previous one. This is why the duplicates don't all end up in the same place. Figure 6.8 shows the result.
Listing 6.8 Repeating Layers Using CAReplicatorLayer
@interface ViewController ()
@property (nonatomic,weak)IBOutlet UIView *containerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//create a replicator layer and add it to our view
CAReplicatorLayer *replicator = [CAReplicatorLayer layer];
replicator.frame = self.containerView.bounds;
[self.containerView.layer addSublayer:replicator];
//configure the replicator
replicator.instanceCount = 10;
//apply a transform for each instance
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, 0, 200, 0);
transform = CATransform3DRotate(transform, M_PI / 5.0, 0, 0, 1);
transform = CATransform3DTranslate(transform, 0, -200, 0);
replicator.instanceTransform = transform;
//apply a color shift for each instance
replicator.instanceBlueOffset = -0.1;
replicator.instanceGreenOffset = -0.1;
//create a sublayer and place it inside the replicator
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(100.0f, 100.0f, 100.0f, 100.0f);
layer.backgroundColor = [UIColor whiteColor].CGColor;
[replicator addSublayer:layer];
}
@end
6 Specialzed layers 特殊层 第二部分 读书笔记的更多相关文章
- 6 Specialzed layers 特殊层 第一部分 读书笔记
6 Specialzed layers 特殊层 第一部分 读书笔记 Specialization is a feature of every complex organization. 专注是 ...
- linux第二次读书笔记
<Linux内核设计与实现>读书笔记 第五章 系统调用 第五章系统调用 系统调用是用户进程与内核进行交互的接口.为了保护系统稳定可靠,避免应用程序恣意忘形. 5.1与内核通信 系统调用 ...
- 《ECMAScript标准入门》第二版读书笔记
title: <ECMAScript标准入门>第二版 date: 2017-04-10 tags: JavaScript categories: Reading-note 2015年6月, ...
- 《细说PHP》第二版--读书笔记
第五章 PHP的基本语法 5.2.4 在程序中使用空白的处理 5.3 变量 5.3.1 变量的声明 在php中变量的声明必须是使用一个$符号,后面跟变量名来表示 unset()函数释放指定变量 iss ...
- 20135320赵瀚青LINUX第二章读书笔记
第二章-从内核出发 获取内核代码 使用git 获取最新提交到版本树的一个副本 $ git clone git://git.kernel.org/pub/scm/linux/kernel/git/tor ...
- Android深度探索--HAL与驱动开发----第二章读书笔记
1. 底层开发工具包括: JDk6或者以上版本:Eclipse3.4或以上版本:ADT(用于开发Android应用程序),CDT(用于开发AndroidNDK程序):Android SDK:Andro ...
- sed&awk第二版读书笔记
1. POSIX标准对正则表达式字符和操作符的含义进行了形式化.这种标准定义了两类正则表达式:基本的正则表达式(BRE),grep和sed使用这种正则表达式;扩展的表达式,egrep和awk使用这种正 ...
- Linux第二章读书笔记
1.获取内核源码 1.1Git 分布式的:下载和管理Linux内核源代码: - 获取最新提交到版本树的一个副本 $ git clone git://git.kernel.org/pub/scm/lin ...
- 2013337朱荟潼 Linux第二章读书笔记——从内核出发
1.获取内核源码 1.1Git 分布式的:下载和管理Linux内核源代码: - 获取最新提交到版本树的一个副本 $ git clone git://git.kernel.org/pub/scm/lin ...
随机推荐
- 2016-5-23 jsp
1.table的边框:rules这个参数,它有三个值(cols,rows,none),当rules=cols时,表格会隐藏横向的分隔线,也就是我们只能看到表格的列:当rules=rows时,就隐藏了纵 ...
- [原创]JAVA获取word表格中数据的方案
上一个项目的开发中需要实现从word中读取表格数据的功能,在JAVA社区搜索了很多资料,终于找到了两个相对最佳的方案,因为也得到了不少网友们的帮助,所以不敢独自享用,在此做一个分享. 两个方案分别是: ...
- Oracle中如何用SQL检测字段是否包括中文字符
用Oracle的编码转换的函数Convert实现,测试后可行. SQL> select * 2 from (select 'abcd' c1 from dual 3 ...
- 如何生成Android的keystore文件
步骤 1 找到本机电脑上jdk安装的目录,使用cmd命令打开命令窗口,输入cd jdk目录(替换成你的jdk的bin目录),进入到jdk的bin目录,接下来你才可以使用jdk的命令进行操作 步骤 ...
- uoj103 apio2014 Palindromes
题目链接:http://uoj.ac/problem/103 题解: 首先,我们可以用后缀自动机算出每个字符串的出现次数.然后我们可以用manacher找出所有不同的回文串(o(n)个),统计答案即可 ...
- 一个C#文件传输模块,支持断点续传
一个C#文件传输模块,支持断点续传 最近做一个程序需要传送文件,在网上找了好久也没找到好用的方案,于是自己写了一个,与大家分享,希望大家帮忙改进,拍砖欢迎-文件采取分块发送,每块单独校验,能够保证文件 ...
- 模态对话框 DoModal的用法 (vs2008)与非模态对话框
Windows对话框分为两类:模态对话框和非模态对话框. 模态对话框,当它弹出后,本应用程序其他窗口将不再接受用户输入,只有该对话框响应用户输入,在对它进行相应操作退出后,其他窗口才能继续与用户交互. ...
- 009--test命令和grep作业及Sed作业awk作业和循环结构
一.test命令 -d :目录 test -d /boot -s :文件长度 > 0.非空 test - ...
- lightoj 1033【区间DP/LCS】
题意: 给你一个长度<=100的字符串. 然后你可以在任何位置插入字符,问最少插入几个构成回文. 思路: 1.长度-LCS: 2.区间DP; 我保证小的区间是一个回文,然后枚举区间,构成大区间, ...
- Unity里的人物驱动/换装备/换武器/换衣服/卡通重定位(转)
Unity里的人物驱动/换装备/换武器/换衣服/动画重定位 刚学的过程被这个问题困扰最多. 首先,基本的,大家都知道驱动人物需要骨架.绑骨的Mesh和动画(这三个要是不知道的话就得考虑看看计算机图形学 ...