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. bootstrap-16

    进度条----基本样式: Bootstrap框架中对于进度条提供了一个基本的样式,一个100%宽度的背景色,然后高亮颜色表示完成进度.其实制作这样的进度条非常容易,一般是使用两个容器,外容器具有一定的 ...

  2. 分享一个ruby网站 | 菜鸟教程

    http://www.runoob.com/ruby/ruby-tutorial.html 分享一个ruby网站.

  3. BLOCK封装带菊花的网络请求

    #import <Foundation/Foundation.h> @class HttpRequestManager; typedef void(^httpRequestBlock) ( ...

  4. LeetCode 【318. Maximum Product of Word Lengths】

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  5. pyside窗口关闭触发事件

    窗口关闭事件本质上是重写了类内部的closeEvent方法,可以通过重写这个类去实现其他你想要的关闭事件. 下面的例子实现了一个简单的窗口,并为窗口添加了关闭时弹出提示框的功能. import sys ...

  6. 【转】关于启用 HTTPS 的一些经验分享

    随着国内网络环境的持续恶化,各种篡改和劫持层出不穷,越来越多的网站选择了全站 HTTPS.HTTPS 通过 TLS 层和证书机制提供了内容加密.身份认证和数据完整性三大功能,可以有效防止数据被查看或篡 ...

  7. Android从零开始--安装

    1.下载安装eclipse.adt和Android sdk(以前一直以为Android使用的sdk也是java jdk呢,呵呵) 2.都安装完成后配置eclipse的Android的环境,将Andro ...

  8. RAID简介

    RAID(独立磁盘冗余阵列)可以提供较普通磁盘更高的速度,安全性,所以服务器在安装时会选择创建RAID.RAID的创建有两种方式:软RAID(通过操作系统软件来实现)和硬raid(使用硬件整列卡) r ...

  9. [2015.08.13]万峰快递单打印软件 v2.2

    <万峰快递单打印软件>是一款简单易用,且支持所有快递单的打印软件.1.支持文字打印位置的坐标精确定位和微调的打印工具.2.操作灵活,只需录入收件人和发件人信息即可打印:3.快递单打印尺寸和 ...

  10. Uva11292--------------(The Dragon of Loowater)勇者斗恶龙 (排序后贪心)

    ---恢复内容开始--- 题目: Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major ...