iOS视图更新与事件循环

iOS使用的是事件循环+标志更新(视图)机制。

The View 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.

Note: Changing a view’s geometry does not automatically cause the system to redraw the view’s content. The view’s contentMode property determines how changes to the view’s geometry are interpreted. Most content modes stretch or reposition the existing snapshot within the view’s boundaries and do not create a new one. For more information about how content modes affect the drawing cycle of your view, see Content Modes.

When the time comes to render your view’s content, the actual drawing process varies depending on the view and its configuration. System views typically implement private drawing methods to render their content. Those same system views often expose interfaces that you can use to configure the view’s actual appearance. For custom UIView subclasses, you typically override the drawRect: method of your view and use that method to draw your view’s content. There are also other ways to provide a view’s content, such as setting the contents of the underlying layer directly, but overriding the drawRect: method is the most common technique.

For more information about how to draw content for custom views, see Implementing Your Drawing Code.

The Runtime Interaction Model for Views

Any time a user interacts with your user interface, or any time your own code programmatically changes something, a complex sequence of events takes place inside of UIKit to handle that interaction. At specific points during that sequence, UIKit calls out to your view classes and gives them a chance to respond on behalf of your application. Understanding these callout points is important to understanding where your views fit into the system. Figure 1-7 shows the basic sequence of events that starts with the user touching the screen and ends with the graphics system updating the screen content in response. The same sequence of events would also occur for any programmatically initiated actions.

Figure 1-7  UIKit interactions with your view objects

The following steps break the event sequence in Figure 1-7 down even further and explain what happens at each stage and how you might want your application to react in response.

  1. The user touches the screen.
  2. The hardware reports the touch event to the UIKit framework.
  3. The UIKit framework packages the touch into a UIEvent object and dispatches it to the appropriate view. (For a detailed explanation of how UIKit delivers events to your views, see Event Handling Guide for iOS.)
  4. The event-handling code of your view responds to the event. For example, your code might:
    • Change the properties (frame, bounds, alpha, and so on) of the view or its subviews.
    • Call the setNeedsLayout method to mark the view (or its subviews) as needing a layout update.
    • Call the setNeedsDisplay or setNeedsDisplayInRect: method to mark the view (or its subviews) as needing to be redrawn.
    • Notify a controller about changes to some piece of data.
  5. Of course, it is up to you to decide which of these things the view should do and which methods it should call.
  6. If the geometry of a view changed for any reason, UIKit updates its subviews according to the following rules:
    • If you have configured autoresizing rules for your views, UIKit adjusts each view according to those rules. For more information about how autoresizing rules work, see Handling Layout Changes Automatically Using Autoresizing Rules.
    • If the view implements the layoutSubviews method, UIKit calls it.You can override this method in your custom views and use it to adjust the position and size of any subviews. For example, a view that provides a large scrollable area would need to use several subviews as “tiles” rather than create one large view, which is not likely to fit in memory anyway. In its implementation of this method, the view would hide any subviews that are now offscreen or reposition them and use them to draw newly exposed content. As part of this process, the view’s layout code can also invalidate any views that need to be redrawn.
  7. If any part of any view was marked as needing to be redrawn, UIKit asks the view to redraw itself.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.
    Standard system views typically do not implement a drawRect: method but instead manage their drawing at this time.
  8. Any updated views are composited with the rest of the application’s visible content and sent to the graphics hardware for display.
  9. The graphics hardware transfers the rendered content to the screen.

https://developer.apple.com/library/archive/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html#//apple_ref/doc/uid/TP40009503-CH2-SW42

原文地址:https://www.cnblogs.com/feng9exe/p/10900185.html

时间: 2024-08-06 00:44:43

iOS视图更新与事件循环的相关文章

vue nextTick深入理解-vue性能优化、DOM更新时机、事件循环机制

一.定义[nextTick.事件循环] nextTick的由来: 由于VUE的数据驱动视图更新,是异步的,即修改数据的当下,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图更新. nextTick的触发时机: 在同一事件循环中的数据变化后,DOM完成更新,立即执行nextTick(callback)内的回调. 应用场景: 需要在视图更新之后,基于新的视图进行操作. 以上出现了事件循环的概念,其涉及到JS的运行机制,包括主线程的执行栈.异步队列.异步API.事件循环的协

【IOS 开发】IOS 开发 简介 (IOS项目文件 | MVC 模式 | 事件响应机制 | Storyboard 控制界面 | 代码控制界面 | Retina 屏幕图片适配)

一. IOS 项目简介 1. IOS 文件简介 创建一个 HelloWorld 项目, 在这个 IOS 项目中有四个目录 : 如下图; -- HelloWorldTests 目录 : 单元测试相关的类和资源; (1) HelloWorld 目录 HelloWorld 目录介绍 : -- 命名规则 : 该目录名称与 IOS 项目名称相同, 是主目录; -- 存放内容 : IOS 项目的 源码文件, 界面设计文件, 资源文件都存放在该目录下; -- 源文件 : Objective C 的 .m 和

iOS 动态更新

iOS 动态更新 App 动态更新 1.控件到 window 的层级关系: 2.分析控件的详细路径: 3.动态修改控件: 4.工具篇: 视图的层级关系: 每个 App , 至少有一个根 Window , 通常情况下我们只用一个 .window 有一个 rootViewController , 这就是我们所谓的根视图 , 我们所有的控制器都是放在 rootViewController 里面的. 这个是最简单的层级关系 如果在项目里有了这么一个路径 , 我们可以做什么呢? 在当项目很复杂 , 可以其

探索未知种族之osg类生物---呼吸分解之事件循环一

事件循环和更新循环**终于到了我们嘴里经常念叨的事件循环.更新循环以及渲染循环了.首先我们来区分一下事件循环和渲染循环,他们两个首先是两个不同顺序执行的过程,我们有时候会用到任意node的updateCallback函数,这个就是在更新循环的时候遍历所有的node来调用updateCallback函数的:而事件循环是与用户操作和操作系统事件想关联的,以及调用我们设置的事件回调(EventCallback)函数.而事件循环函数(viewer::eventTraversal())是我们现在要探究的内

Muduo网络库源码分析(一) EventLoop事件循环(Poller和Channel)

从这一篇博文起,我们开始剖析Muduo网络库的源码,主要结合<Linux多线程服务端编程>和网上的一些学习资料! (一)TCP网络编程的本质:三个半事件 1. 连接的建立,包括服务端接受(accept) 新连接和客户端成功发起(connect) 连接.TCP 连接一旦建立,客户端和服务端是平等的,可以各自收发数据. 2. 连接的断开,包括主动断开(close 或shutdown) 和被动断开(read(2) 返回0). 3. 消息到达,文件描述符可读.这是最为重要的一个事件,对它的处理方式决定

Sharepoint2010 列表默认视图更新

Sharepoint2010 列表默认视图更新 1.简介 利用cmdlet脚本管理sharepoint项目是很方便的,但是对对于脚本语言接触较少的我来说写起来废了很大力气.需求  利用sharepoint开发门户管理,客户那边提出要求文档可默认视图每页显示15条,图片库每页显示12条.由于创建的站点.子网站.库很多.要是一个个改那样很容易漏掉摸个库,而且说不定改天又心情不爽又要修改每页显示的条数.所以写个脚本跑下解决问题吧 2.指令集讲解 这里先对有的指令简单说下 ForEach-Object{

ios &ndash; 视图控制器如何管理视图?

移动设备的屏幕有限,所有的东西都需要放到一个单一窗口组成的单一界面显示,在ios中体现为视图切换(在<ios – 视图>中已经说明了视图),当一个视图替换掉另一个视图的时候,会经常使用动画效果,这个任务就是交给视图管理器来完成的. ios5之后应用程序窗口有一个根视图控制器(rootViewController),当不为rootViewController赋值时,会出现"Application windows are expected to have a root view cont

iOS开发UI篇—事件传递

iOS开发UI篇—事件传递 一.事件的产生和传递 发生触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中 UIApplication会从事件队列中取出最前面的事件,并将事件分发下去以便处理,通常,先发送事件给应用程序的主窗口(keyWindow) 主窗口会在视图层次结构中找到一个最合适的视图来处理触摸事件 找到合适的视图控件后,就会调用视图控件的touches方法来作具体的事件处理 touchesBegan… touchesMoved… touchedEnded…

iOS UIButton长按事件

//UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeCustom];//[aBtn setFrame:CGRectMake(40, 100, 60, 60)]; [aBtn setBackgroundImage:[UIImage imageNamed:@"111.png"] forState:UIControlStateNormal];//button点击事件 [aBtn addTarget:self action:@select