UIView 的绘制模式是按需要(on-demand),当这个View第一次出现在屏幕上时,系统请求View绘制他的content,然后系统缓存该snapshot
When a view first appears on
the screen, the system asks it to draw its content. The system captures a snapshot of this content and uses
that snapshot as the view’s visual representation. If you never change the view’s content, the view’s drawing
code may never be called again. The snapshot image is reused for most operations involving the view. If you
do change the content, you notify the system that the view has changed. The view then repeats the process
of drawing the view and capturing a snapshot of the new results.
When the contents of your view change, you do not redraw those changes directly. Instead, you invalidate the
view using either the setNeedsDisplay or setNeedsDisplayInRect: method. These methods tell the
system that the contents of the view changed and need to be redrawn at the next opportunity. The system
waits until the end of the current run loop before initiating any drawing operations. This delay gives you a
chance to invalidate multiple views, add or remove views from your hierarchy, hide views, resize views, and
reposition views all at once. All of the changes you make are then reflected at the same time
本文深入解析了UIView的绘制机制,包括其按需绘制的特点及如何通过快照(snapshot)缓存来提高视图更新效率。文章解释了当UIView内容发生变化时,如何通过调用setNeedsDisplay或setNeedsDisplayInRect:方法通知系统重新绘制,并介绍了系统如何延迟执行这些绘制操作以提高性能。
120

被折叠的 条评论
为什么被折叠?



