Drawing Cycle
The UIView class uses an on-demand drawing model for presenting content. 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.
当view第一次出现在屏幕上的时候,系统会要求view绘制自己的内容,并且抓取一份快照保存,如果view的内容始终没有发生变化,则view不会再调用自己的绘制方法,系统会使用之前保存的快照进行view的显示.当view的内容发生变化的时候,通常需要手动调用
setNeedsDisplay或者 setNeedsDisplayInRect:方法,告知系统需要重绘这个view.
注意:通常来说,重写 drawRect:方法是绘制一个view的内容的最好的实现方式
Interaction Model for Views

- 用户触摸view
- 硬件系统将触摸事件发送给 UIKit framework
- UIKit framework 将事件打包成一个 UIEvent object ,然后找到最合适的view.
- 目标view对事件做出响应
- 如果view的frame发生变化,那么更新view及其子视图的frame
- 如果需要重绘,那么调用重绘方法
For custom views that explicitly define a drawRect:
method, UIKit calls that method. Your implementation of this method should redraw the specified area of the view as quickly as possible and nothing else. Do not make additional layout changes at this point and do not make other changes to your application’s data model. The purpose of this method is to update the visual content of your view
- 所有更新完成后通知硬件系统去显示
- 硬件系统将更新后的视图渲染到屏幕上
Monitoring Window Changes
可以使用下面几个通知监视 UIWindow 的相关变化.
UIWindowDidBecomeVisibleNotification
UIWindowDidBecomeHiddenNotification
UIWindowDidBecomeKeyNotification
UIWindowDidResignKeyNotification
有一点需要注意的是,当App进入后台的时候,并不会触发UIWindowDidBecomeHiddenNotification
Converting Coordinates in the View Hierarchy(坐标转换)
主要用到下面4个方法
convertPoint:fromView:
convertRect:fromView:
convertPoint:toView:
convertRect:toView:
Creating Animated Transitions Between Views
对某个子view做过渡动画
use the transitionWithView:duration:options:animations:completion:
method to initiate a transition animation for a view. In the animations block passed to this method, the only changes that are normally animated are those associated with showing, hiding, adding, or removing subviews
Replacing a View with a Different View
对父视图中的子View做过渡动画,比如让某个view显示的同时,另一个view消失
use the transitionFromView:toView:duration:options:completion:
method to transition between two views. This method actually removes the first view from your hierarchy and inserts the other, so you should make sure you have a reference to the first view if you want to keep it. If you want to hide views instead of remove them from your view hierarchy, pass the UIViewAnimationOptionShowHideTransitionViews
key as one of the options.
网友评论