iOS 进阶文集iOS进阶指南iOS学习开发

CALayer的小计!

2016-07-28  本文已影响123人  繁华落尽丶lee

CALayer

contents属性

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

/* 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类型。

layer = (__bridge id)image.CGImage;

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

contentsGravity

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


/* 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。


/* 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

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

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

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

contentsCenter

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

@property CGRect contentsCenter;

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


contentsCenter.png

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

cornerRadius

//默认为0
@property CGFloat cornerRadius;

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

borderWidth和boarderColor

//默认为 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

上一篇 下一篇

猜你喜欢

热点阅读