重画视图------setNeedsDisplay

1.当UIView实例收到setNeedsDisplay消息后,相应的视图会被标记为“需要重画”,当View的绘图内容发生改变时,会给自己发送setNeedsDisplay消息,例如:UITextField被发送setText:消息时会把自己标记为“需要重画”。

2.但是一个View被标记为“需要重画”,并不会马上重画,而是会被加到一个“需要更新的views”队列当中。应用程序是一个巨大的无限的循环叫做“运行循环”,运行循环的工作是检查输入(前面的内存管理已经提到过),当有点击事件,或其他比如网络事件,地图更新事件时会去调用响应事件的方法(比如点击按钮后执行的方法)。当然方法里面也会有另外的方法,当这些事件都执行完了,程序再次回到循环当中时,准备好必要的“绘图上下文”,然后向标记的视图发送drawRect:。

3.当view重画完了,它的子视图也会自动的被迫使重画。(这里应该是父视图向子视图发送drawRect:消息)。

(原文:When a UIView instance is sent the message setNeedsDisplay, that view is marked for re-display. View subclasses send themselves setNeedsDisplay when their drawable content changes. For example, UITextField will mark itself for re-display if it is sent the message setText:. (It has to redraw if the text it displays changes.) When a view has marked itself for re-display, it is not immediately redrawn; instead, it is added to a list of views that need updating. Your application is a giant infinite loop called the run loop. The run)loop’s job is to check for input (a touch, Core Location updates, data coming in through a network interface, etc.) and then find the appropriate handlers for that event (like an action or delegate methodfor an object). Those handler methods call other methods, which call more methods, and so on. Views are not redrawn until after the methods have completed and control returns to the run loop, as shown in Figure 6.8.


When control returns to the run loop, it says, “Well, a bunch of code was just executed. I’m going to check if any views need to be redrawn.” Then the run loop prepares the necessary drawing contexts and sends the message drawRect: to all of the views that have been sent setNeedsDisplay in this iteration of the loop. After a view has redrawn itself, its subviews are automatically asked to redraw themselves as well.

由上图也可以看出redraws(大方块下面的箭头指向)是在跳出了这些方法之后才执行的,在这些事件方法中有的view会被发送setNeedsDisplay。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值