版本记录
版本号 | 时间 |
---|---|
V1.0 | 2018.01.23 |
前言
在苹果的API文档中,有很多属性和方法我们用的不是很多,所以很容易忽略和出错,下面我就用这一个专题专门说一些不常用的API接口,下面开始。感兴趣的可以参考前面几篇文章。
1. 容易忽略的那些小点总结 (一) —— UIView UIViewTintAdjustmentMode相关(一)
2. 容易忽略的那些小点总结 (二) —— CALayer相关(一)
3. 容易忽略的那些小点总结 (三) —— CALayer相关(二)
4. 容易忽略的那些小点总结 (四) —— CALayer相关(三)
5. 容易忽略的那些小点总结 (五) —— CALayer相关(四)
6. 容易忽略的那些小点总结 (六) —— CALayer相关(五)
allowsGroupOpacity
在 iOS7 中,这个属性表示 layer 的 sublayer 是否继承父 layer 的透明度,主要用途是当在动画中改变一个 layer 的透明度时(会引起子 view 的透明度显示出来)。但是如果你不需要这种绘制类型,可以关闭这个属性来提高性能。先看一下这个属性的API文档。
/* When true, and the layer's opacity property is less than one, the
* layer is allowed to composite itself as a group separate from its
* parent. This gives the correct results when the layer contains
* multiple opaque components, but may reduce performance.
*
* The default value of the property is read from the boolean
* UIViewGroupOpacity property in the main bundle's Info.plist. If no
* value is found in the Info.plist the default value is YES for
* applications linked against the iOS 7 SDK or later and NO for
* applications linked against an earlier SDK. */
如果为true,并且该图层的不透明属性小于1,则允许图层将其自身组合为与其父级分离的组。
当图层包含多个不透明的组件时,这会给出正确的结果,但可能会降低性能。 该属性的默认值是
从主包的Info.plist中的布尔值UIViewGroupOpacity属性中读取的。 如果在Info.plist中
没有找到任何值,则针对iOS 7 SDK或更高版本链接的应用程序的缺省值为YES,而针对早期SDK链接的应用程序为NO。
@property BOOL allowsGroupOpacity;
还有几点需要注意:
CALayer的
allowsGroupOpacity
属性,UIView 的alpha属性等同于 CALayer opacity属性。GroupOpacity=YES,子 layer 在视觉上的透明度的上限是其父 layer 的opacity。当父视图的layer.opacity != 1.0时,会开启离屏渲染。当layer.opacity == 1.0
时,父视图不用管子视图,只需显示当前视图即可。为了让子视图与父视图保持同样的透明度,从 iOS 7 以后默认全局开启了这个功能。
compositingFilter
CoreImage过滤器,用于复合图层及其背后的内容,可动画。
/* A filter object used to composite the layer with its (possibly
* filtered) background. Default value is nil, which implies source-
* over compositing. Animatable.
*
* Note that if the inputs of the filter are modified directly after
* the filter is attached to a layer, the behavior is undefined. The
* filter must either be reattached to the layer, or filter properties
* should be modified by calling -setValue:forKeyPath: on each layer
* that the filter is attached to. (This also applies to the `filters'
* and `backgroundFilters' properties.) */
用于将图层与其背景(可能已过滤)进行合成的过滤器对象,默认值为nil,这意味着基于源的
合成,可动画。 请注意,如果过滤器的输入在过滤器连接到图层后被直接修改,则行为是不确定的。
过滤器必须重新附加到图层,或者应通过在过滤器所附的每个图层上调用-setValue:forKeyPath:
来修改过滤器属性。 (这也适用于`filters'和`backgroundFilters'属性。)
@property(nullable, strong) id compositingFilter;
还有几点需要说明:
此属性的默认值为nlil,这会导致图层使用源合成。 尽管可以使用任何Core Image滤镜作为图层的合成滤镜,但为了获得最佳效果,请使用CICategoryCompositeOperation类别中的滤镜。
在macOS中,可以在将滤波器附加到图层后修改过滤器的参数,但是必须使用图层的
setValue:forKeyPath:
方法。 例如,要更改过滤器的inputRadius
参数,可以使用类似于以下内容的代码:
CIFilter *filter = ...;
CALayer *layer = ...;
layer.compositingFilter = filter;
[layer setValue:[NSNumber numberWithInt:1] forKeyPath:@"compositingFilter.inputRadius"];
- 在将
CIFilter
对象连接到图层后,直接更改其输入会导致未定义的行为。该属性不支持iOS的图层。
filters
应用于图层及其子图层内容的Core Image
过滤器数组,可动画。
/* An array of filters that will be applied to the contents of the
* layer and its sublayers. Defaults to nil. Animatable. */
一组应用于图层及其子图层的滤镜,默认值是nil,可动画。
@property(nullable, copy) NSArray *filters;
还有几点需要注意:
添加到此属性的过滤器会影响图层的内容,包括其边框,填充的背景和子图层。 此属性的默认值为零。
在将 CIFilter对象连接到图层后,直接更改其输入会导致未定义的行为。 将滤镜附加到图层后可以修改滤镜参数,但是您必须使用该图层的
setValue:forKeyPath:
方法。 另外,您必须为过滤器指定一个名称,以便您可以在数组中识别它。 例如,要更改过滤器的inputRadius
参数,可以使用类似于以下内容的代码:
CIFilter *filter = ...;
CALayer *layer = ...;
filter.name = @"myFilter";
layer.filters = [NSArray arrayWithObject:filter];
[layer setValue:[NSNumber numberWithInt:1] forKeyPath:@"filters.myFilter.inputRadius"];
- iOS中的图层不支持此属性。
backgroundFilters
Core Image
过滤器数组,适用于图层后面的内容。动画。
/* An array of filters that are applied to the background of the layer.
* The root layer ignores this property. Animatable. */
一个可以应用到图层背景的一组滤镜,根图层忽略这个属性,可动画。
@property(nullable, copy) NSArray *backgroundFilters;
还有几点需要注意:
背景滤镜会影响显示在图层本身之后的图层的内容。 通常这个内容属于作为图层的父层的超层。 这些过滤器不会影响图层本身的内容,包括图层的背景颜色和边框。
此属性的默认值为零。
在将
CIFilter
对象连接到图层后,直接更改其输入会导致未定义的行为。 在macOS中,可以在将图层附加到图层后修改滤镜参数,但是您必须使用图层的setValue:forKeyPath:
方法来执行此操作。 另外,您必须为过滤器指定一个名称,以便您可以在数组中识别它。 例如,要更改过滤器的inputRadius
参数,可以使用类似于以下内容的代码:
CIFilter *filter = ...;
CALayer *layer = ...;
filter.name = @"myFilter";
layer.backgroundFilters = [NSArray arrayWithObject:filter];
[layer setValue:[NSNumber numberWithInt:1] forKeyPath:@"backgroundFilters.myFilter.inputRadius"];
- 您可以使用图层的 masksToBounds来控制其背景滤镜效果的范围,iOS中的图层不支持此属性。
shouldRasterize
一个布尔值,指示在合成之前该图层是否呈现为位图,可动画。
/* When true, the layer is rendered as a bitmap in its local coordinate
* space ("rasterized"), then the bitmap is composited into the
* destination (with the minificationFilter and magnificationFilter
* properties of the layer applied if the bitmap needs scaling).
* Rasterization occurs after the layer's filters and shadow effects
* are applied, but before the opacity modulation. As an implementation
* detail the rendering engine may attempt to cache and reuse the
* bitmap from one frame to the next. (Whether it does or not will have
* no affect on the rendered output.)
*
* When false the layer is composited directly into the destination
* whenever possible (however, certain features of the compositing
* model may force rasterization, e.g. adding filters).
*
* Defaults to NO. Animatable. */
如果为true,则图层将在其局部坐标空间(“栅格化”)中呈现为位图bitmap,然后将位图
合成到目标中(如果位图需要缩放,则应用图层的minificationFilter和magnificationFilter属性)。
在应用图层的过滤器和阴影效果之后,但在不透明度调制之前发生栅格化。 作为一个实现细节,渲
染引擎可能会试图缓存和重用从一帧到下一帧的位图。 (无论是否对渲染的输出没有任何影响)。
如果为false,则尽可能将图层直接合成到目标中(但是,合成模型的某些功能可能会强制进行栅格化,
例如添加过滤器filters)。 默认为NO,可动画。
@property BOOL shouldRasterize;
还有几点需要注意:
在给图片加圆角的时候,可以很好的利用这个属性,当
shouldRasterize
设成 true 时,layer 被渲染成一个 bitmap,并缓存起来,等下次使用时不会再重新去渲染了。实现圆角本身就是在做颜色混合(blending),如果每次页面出来时都blending,消耗太大,这时shouldRasterize = yes
,下次就只是简单的从渲染引擎的 cache 里读取那张 bitmap,节约系统资源。对于layer的
shouldRasterize
属性默认为NO,将此属性设置为YES时,会在对应的本地坐标空间创建一张bitmap图片,用来缓存图片,当在layer上添加filters
或者shadow effects
等特效时,将自动开启光栅化的功能,光栅化的开启与否不会影响最终的render output ,即最终的渲染输出,但是有一点要注意,开启光栅化虽然不会影响最终的输出结果,但是却可能严重影响性能。
rasterizationScale
栅格化内容的比例,相对于图层的坐标空间,可动画。
/* The scale at which the layer will be rasterized (when the
* shouldRasterize property has been set to YES) relative to the
* coordinate space of the layer. Defaults to one. Animatable. */
将图层相对于图层的坐标空间进行栅格化的比例(当shouldRasterize属性设置为YES时)。 默认为1,可动画。
@property CGFloat rasterizationScale;
还有几点需要注意:
当
shouldRasterize
属性中的值为YES时,图层使用此属性来确定是否缩放栅格化内容(以及多少)。 此属性的默认值为1.0,表示该图层应当按其当前大小进行栅格化。 较大的值放大内容,较小的值缩小内容。一般都像下面这样设置圆角
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
参考文章
后记
本篇已结束,后面更精彩~~~