Core Animation学习笔记—第七节Improving Animation Performance

各位iOS开发大佬们好:
我是一名swift+swiftUI栈的iOS小白,目前还在上大三,最近准备实习,面试的过程中发现现在大公司很多还在用OC + UIKit的技术栈,OC我还在考虑要不要学,目前想先把UIKit学完,这是我在官网学习UIKit英文文档时摘录的本人认为的重点,如果你们也觉得对你们有用的话欢迎持续关注,我大概一天更一节,有事除外。格式什么的我也就不做了,翻译都是我自己翻译的,哪里不对欢迎在评论区指正,感谢各位大佬支持
今天2021年12月7日
这一章叫做Improving Animation Performance

这节内容很少,讲的是提高性能的方法

Choose the Best Redraw Policy for Your OS X Views
The default redraw policy for the NSView class preserves the original drawing behavior of that class, even if the view is layer-backed. If you are using layer-backed views in your app, you should examine the redraw policy choices and choose the one that offers the best performance for your app. In most cases, the default policy is not the one that is likely to offer the best performance. Instead, the NSViewLayerContentsRedrawOnSetNeedsDisplay policy is more likely to reduce the amount of drawing your app does and improve performance. Other policies might also offer better performance for specific types of views.
默认的重绘规则执行的是类的原始绘制行为
OS X中并不是每个view都有layer(详见前章节),如果view是有layer的view,那么需要确定哪个重绘规则性能最佳(一般默认的不是最优的),NSViewLayerContentsRedrawOnSetNeedsDisplay 规则减少app的绘制量从而提升性能,其他特定的类可能有更好的选择

In OS X v10.8 and later, views have two options for updating the underlying layer’s contents. When you update a layer-backed view in OS X v10.7 and earlier, the layer captures the drawing commands from the view’s drawRect: method into the backing bitmap image. Caching the drawing commands is effective but is not the most efficient option in all cases. If you know how to provide the layer’s contents directly without actually rendering them, you can use the updateLayer method to do so.
图层将绘图命令从视图的drawRect:方法捕获到备份位图映像中。缓存绘图命令并非在所有情况下都是最有效的选项。如果你知道如何在不渲染的情况下直接提供图层的内容,可以使用updateLayer方法实现。

Use Opaque Layers Whenever Possible
Setting the opaque property of your layer to YES lets Core Animation know that it does not need to maintain an alpha channel for the layer. Not having an alpha channel means that the compositor does not need to blend the contents of your layer with its background content, which saves time during rendering. However, this property is relevant primarily for layers that are part of a layer-backed view or situations where Core Animation creates the underlying layer bitmap. If you assign an image directly to the layer’s contents property, the alpha channel of that image is preserved regardless of the value in the opaque property
再下来就是说了八百遍的,设置不透明避免渲染视图下方的部分提高性能,如果layer的content赋值为一个image,意味着alpha channel会被一直保持,所以不管不透明属性是否设置为YES,都会混合视图下方部分的内容

Use Simpler Paths for CAShapeLayer Objects
The CAShapeLayer class creates its content by rendering the path you provide into a bitmap image at composite time. The advantage is that the layer always draws the path at the best possible resolution but that advantage comes at the cost of additional rendering time. If the path you provide is complex, rasterizing that path might get too expensive. And if the size of the layer changes frequently (and thus must be redrawn frequently), the amount of time spent drawing can add up and become a performance bottleneck.
One way to minimize drawing time for shape layers is to break up complex shapes into simpler shapes. Using simpler paths and layering multiple CAShapeLayer objects on top of one another in the compositor can be much faster than drawing one large complex path. That is because the drawing operations happen on the CPU whereas compositing takes place on the GPU. As with any simplifications of this nature, though, the potential performance gains are dependent on your content. Therefore, it is especially important to measure the performance of your code before optimizing so that you have a baseline to use for comparisons.
简化CAShapeLayer的渲染路径
CAShapeLayer是通过渲染位图中的路径显示内容的,所以要尽量简化渲染路径

Assigning an image to the contents property prevents the layer from allocating memory for a backing store. Instead, the layer uses the image you provide as its backing store. When several layers use the same image, this means that all of those layers are sharing the same memory rather than allocating a copy of the image for themselves.
直接给content属性赋值image避免layer申请内存,适用于多个layer使用同一image减少内存使用提高性能

Although you specify the width and height of your layer’s bounds using floating-point numbers, the layer bounds are ultimately used to create a bitmap image. Specifying integral values for the width and height simplifies the work that Core Animation must do to create and manage the backing store and other layer information
给width和height赋值整型变量减少计算量

Try enabling the drawsAsynchronously property on your layer to move those operations to a background thread. If you do so, make sure your drawing code is thread safe.
使用drawsAsynchronously 属性将渲染操作移去其他线程,需要保证线程安全

Specify a Shadow Path When Adding a Shadow to Your Layer
Letting Core Animation determine the shape of a shadow can be expensive and impact your app’s performance. Rather than letting Core Animation determine the shape of the shadow, specify the shadow shape explicitly using the shadowPath property of CALayer. When you specify a path object for this property, Core Animation uses that shape to draw and cache the shadow effect. For layers whose shape never changes or rarely changes, this greatly improves performance by reducing the amount of rendering done by Core Animation.
Layer添加阴影时指定阴影路径
由Core Animation决定阴影的形状非常消耗性能,通过使用CALayer的shadowPath 属性显式指定阴影形状

猜你喜欢

转载自blog.csdn.net/Programmer_Roy/article/details/121702571