CALayer的那些坑!

CALayer

contents属性

CALayer有一个属性叫做contents,这个属性是id类型,可以接收任意类型的对象。

1
2
3
4
5
6
7
/* An object providing the contents of the layer, typically a CGImageRef,
* but may be something else. (For example, NSImage objects are
* supported on Mac OS X 10.6 and later.) Default value is nil.
* Animatable. */

@property(nullable, strong) id contents;

但是我们在实际应用中只能赋CGImage,否则图层将是空白的。这是因为Mac OS的历史遗留下来的问题,在Mac OS系统中,contents可以接收CGImage和NSImage类型的值,但是在iOS系统中,则不支持UIImage类型的值。

contents实际上接收的是值是CGImageRef,它是指向CGImage结构的指针。UIImage有一个CGImage属性,返回值为CGImageRef。这么来说我们可以将UIImage的CGImage直接赋值给contents?但是这样会报错,因为CGImageRef并不是一个Cocoa对象,而是一个Core Foundation类型。

1
2
layer = (__bridge id)image.CGImage;

我们需要使用(__bridge id) 在ARC状态下,转换为id类型。

contentsGravity

contentsGravity类似UIImageView的contentMode属性,但是它是NSString类型的。

1
2
3
4
5
6
7
8
9
10

/* A string defining how the contents of the layer is mapped into its
* bounds rect. Options are `center', `top', `bottom', `left',
* `right', `topLeft', `topRight', `bottomLeft', `bottomRight',
* `resize', `resizeAspect', `resizeAspectFill'. The default value is
* `resize'. Note that "bottom" always means "Minimum Y" and "top"
* always means "Maximum Y". */

@property(copy) NSString *contentsGravity;

contentsGravity作用和contentMode相似,都是为了决定内容在图层中的对齐方式。contentsGravity有很多可选常量值,例如: kCAGravityCenter。

contentsScale

contentsScale属性定义了缩放比例。默认为 1.0。

1
2
3
4
5
6
7
8
9
10
11

/* Defines the scale factor applied to the contents of the layer. If
 * the physical size of the contents is '(w, h)' then the logical size
 * (i.e. for contentsGravity calculations) is defined as '(w /
 * contentsScale, h / contentsScale)'. Applies to both images provided
 * explicitly and content provided via -drawInContext: (i.e. if
 * contentsScale is two -drawInContext: will draw into a buffer twice
 * as large as the layer bounds). Defaults to one. Animatable. */

@property CGFloat contentsScale

实际使用的时候,会发现改变contentsScale大小没有明显变化。其实这个属行与分辨率有关,如果值为1.0,将会以每个点1个像素绘制图片,当设置为2.0,将会每个点2个像素绘制图片。Retina屏幕中,需要设置这个属性。

masksToBounds

masksToBounds属性用来决定是否显示超出边界的内容,与UIView中clipsToBounds作用相同。

contentsRect

1
2
3
/* Defaults to the unit rectangle [0 0 1 1]. Animatable. */
@property CGRect contentsRect;

contentsRect属性允许图层边框里显示一个子域。涉及到图片是如何显示和拉伸的,比contentsGravity灵活。
和bounds, frame不同,contentsRect不是按点计算的,它是使用的单位坐标([0 ,1]),是一个相对值。它是相对以寄宿图的尺寸的。

  • 点 —— 在iOS和Mac OS中最常见的坐标体系。点就像是虚拟的像素,也被称作逻辑像素。在标准设备上,一个点就是一个像素,但是在Retina设备上,一个点等于2*2个像素。iOS用点作为屏幕的坐标测算体系就是为了在Retina设备和普通设备上能有一致的视觉效果。
  • 像素 —— 物理像素坐标并不会用来屏幕布局,但是仍然与图片有相对关系。UIImage是一个屏幕分辨率解决方案,所以指定点来度量大小。但是一些底层的图片表示如CGImage就会使用像素,所以你要清楚在Retina设备和普通设备上,它们表现出来了不同的大小。
  • 单位 —— 对于与图片大小或是图层边界相关的显示,单位坐标是一个方便的度量方式, 当大小改变的时候,也不需要再次调整。单位坐标在OpenGL这种纹理坐标系统中用得很多,Core Animation中也用到了单位坐标。

contentsRect默认是{0,0,1,1}。使用contentsRect可以实现图片拼合功能。

contentsCenter

如果你认为这个跟图片的位置有关,那你应该是被名字误导了。contentsCenter其实是一个CGRect,它定义了一个固定的边框和一个在图层上可以拉伸的区域。

1
2
@property CGRect contentsCenter;

contentsCenter属性默认{0,0,1,1},如果大小改变(contentsGravity改变)改变,那么图片就会被均匀的拉伸。如果我们更改cententsCenter值,则会是图片拉伸区域改变,例如:设置为{0.25,0.25,0.5,0.5}
contentsCenter.png

如果图片拉伸,则只会拉伸绿色区域,其他区域不会改变。

cornerRadius

1
2
3
//默认为0
@property CGFloat cornerRadius;

cornerRadius用来控制图层角的曲率,默认值为0。默认情况下,cornerRadius只影响背景色而不影响北背景图片或者子图层。如果把masksToBounds设置成YES,图层中所有的内容都会被裁切。

borderWidth和boarderColor

1
2
3
4
5
//默认为 0
@property CGFloat borderWidth;
//默认为黑色
@property(nullable) CGColorRef borderColor;

borderWidth和borderColor,共同定义了图层边框的绘制样式。需要注意,borderColor为CGColorRef类型,声明CGColorRef类型属性是使用assign关键字声明。

shadow

阴影可以达到图层深度暗示效果,也可以强调正在显示的图层的优先级。shadowOpacity属性,给定一个值(0-1之间)就可以在任意图层下显示阴影。另外还有三个属性:shadowColor, shadowOffset和shadowRadius。

小结

整理CALayer常用的一些属性,以及使用这些属性应该注意的问题。

参考资料

CALayer - Apple
iOS-Core-Animation-Advanced-Techniques