When an iOS application is launched, it starts a run loop. The run loop’s job is to listen for events,
such as a touch. When an event occurs, the run loop then finds the appropriate handler methods for the
event. Those handler methods call other methods, which call more methods, and so on. Once all of the
methods have completed, control returns to the run loop.
When the run loop regains control, it checks a list of “dirty views” – views that need to be rerendered
based on what happened in the most recent round of event handling. The run loop then sends
the drawRect: message to the views in this list before all of the views in the hierarchy are composited
together again.

These two optimizations – only re-rendering views that need it and only sending drawRect: once per
event – keep iOS interfaces responsive.

To get a view on the list of dirty views, you must send it the message setNeedsDisplay. The
subclasses of UIView that are part of the iOS SDK send themselves setNeedsDisplay whenever their
content changes.

For example, an instance of UILabel will send itself setNeedsDisplay when it is sent
setText:, since changing the text of a label requires the label to re-render its layer. In custom UIView
subclasses, you must send this message yourself.

There is another possible optimization when redrawing: you can mark only a portion of a view as
needing to be redrawn. This is done by sending setNeedsDisplayInRect: to a view.

Often, you will want to do some extra initialization of the subviews that are defined in the XIB file
before they appear to the user. However, you cannot do this in the view controller’s initializer
because the NIB file has not yet been loaded. If you try, any pointers that the view controller declares
that will eventually point to subviews will be pointing to nil. The compiler will not complain if you
send a message to one of these pointers, but whatever you intended to happen to that view object will
not happen.
So where can you access a subview? There are two main options, depending on what you need to do.
The first option is the viewDidLoad method that you overrode to spot lazy loading. The view
controller receives this message after the view controller’s NIB file is loaded, at which point all of
the view controller’s pointers will be pointing to the appropriate objects. The second option is
another UIViewController method viewWillAppear:. The view controller receives this message just
before its view is added to the window.
What is the difference? You override viewDidLoad if the configuration only needs to be done once
during the run of the app. You override viewWillAppear:

if you need the configuration to be done and
redone every time the view controller appears on screen.

View & draw的更多相关文章

  1. 自定义View Draw过程(4)

    目录 目录 1. 知识基础 具体请看我写的另外一篇文章:自定义View基础 - 最易懂的自定义View原理系列 2. draw过程作用 绘制View视图 3. draw过程详解 同measure.la ...

  2. View学习(四)-View的绘制(draw)过程

    View的draw过程相比之于measrue过程,也是比较简单的.并且在我们自定义View时,也经常需要重写onDraw方法,来绘制出我们要实现的效果. 如之前的文章所说,绘制的流程也是起始于View ...

  3. Activtiy完全解析(三、View的显示过程measure、layout、draw)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52840065 本文出自:[openXu的博客]   在Activity完全解析的第一篇文章A ...

  4. Android View框架总结(六)View布局流程之Draw过程

    请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52236145 View的Draw时序图 ViewRootImpl.p ...

  5. Android View 绘制流程(Draw) 完全解析

    前言 前几篇文章,笔者分别讲述了DecorView,measure,layout流程等,接下来将详细分析三大工作流程的最后一个流程——绘制流程.测量流程决定了View的大小,布局流程决定了View的位 ...

  6. Android View 的绘制流程之 Layout 和 Draw 过程详解 (二)

    View 的绘制系列文章: Android View 的绘制流程之 Measure 过程详解 (一) Android View 绘制流程之 DecorView 与 ViewRootImpl 在上一篇  ...

  7. 虾扯蛋:Android View动画 Animation不完全解析

    本文结合一些周知的概念和源码片段,对View动画的工作原理进行挖掘和分析.以下不是对源码一丝不苟的分析过程,只是以搞清楚Animation的执行过程.如何被周期性调用为目标粗略分析下相关方法的执行细节 ...

  8. 深入理解 Android 之 View 的绘制流程

    概述 本篇文章会从源码(基于Android 6.0)角度分析Android中View的绘制流程,侧重于对整体流程的分析,对一些难以理解的点加以重点阐述,目的是把View绘制的整个流程把握好,而对于特定 ...

  9. Android View的绘制流程

    写得太好了,本来还想自己写的,奈何肚里墨水有限,直接转吧.正所谓前人种树,后人乘凉.. View的绘制和事件处理是两个重要的主题,上一篇<图解 Android事件分发机制>已经把事件的分发 ...

随机推荐

  1. easyui datebox 设置不可编辑

    easyui datebox不允许编辑可以输入 editable="false"<input class="easyui-datebox" editabl ...

  2. js代码实现下拉菜单

    效果 js代码: <script type="text/javascript"> function ShowSub(li) {//函数定义 var subMenu = ...

  3. HttpClient封装工具类

    import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.List; ...

  4. NOIP2016の遊記

    看了cydiater的游记,我更加认识到我有多弱,大神有多强 剩余的时间不多了,NOIP前停的一周课又颓了相当多的时间,感觉NOIP真的药丸 最后一天复习模板,看一下错误,总结做题的经验,现在实力实在 ...

  5. Java中的构造代码块

    代码块 ----a静态代码块 ----b构造代码块 ----c普通代码块 执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. a.静态代码块: 静态代码块 ...

  6. Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  7. 2016_09_21 Russia is seriously running out of cash_CNN

    After almost two years in recession,the country's rainy day fund has shrunk to just $32.2 billlion t ...

  8. C# exe dll防止反编译-- dotNET_Reactor

    新建两个VS C#工程test.sln 和 reactor.sln test工程代码中初始化时,删除reactor.exe 生成test工程 用软件 dotNET_Reactor 设置,保存成 set ...

  9. multipath test

    [root@rac02 mnt]# multipath -llmpathb (36005076300810df6f800000000000005) dm-3 IBM,2145size=50G feat ...

  10. Maven最佳实践:Maven仓库(转)

    转自:http://juvenshun.iteye.com/blog/359256 什么是Maven仓库 在不用Maven的时候,比如说以前我们用Ant构建项目,在项目目录下,往往会看到一个名为/li ...