The Runtime Interaction Model for Views-UI布局事件处理流程

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-7shows 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.

    Of course, it is up to you to decide which of these things the view should do and which methods it should call.

  5. If the geometry of a view changed for any reason, UIKit updates its subviews according to the following rules:
    1. 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.
    2. 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.

  6. 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.

  7. Any updated views are composited with the rest of the application’s visible content and sent to the graphics hardware for display.
  8. The graphics hardware transfers the rendered content to the screen.

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

时间: 2024-10-16 20:53:30

The Runtime Interaction Model for Views-UI布局事件处理流程的相关文章

iOS-屏幕适配-UI布局

iOS 屏幕适配:autoResizing autoLayout和sizeClass 一.图片解说 -------------------------------------------------------------------------------------------------------------------------------------------------------------- 二.AutoLayout 1.前言 •在iOS程序中,大部分视图控制器都包含了大量

关于android UI布局自适应

原以为上篇就是农历年到来前写的最后一篇了,但是看来现在还是有必要继续把看到的有用的记录一下,算是比较基础的了,以前没怎么关注. 言归正传,这篇主要说下android的自适应的一点东西,为什么会忽然想起来这个,主要还是因为之前看launcher的代码,其中看到的这段代码始终不明白,所以查了下资料.看下代码, BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; Bitmap bitmap = bitmapDrawable.getBitmap(

Duilib源码分析(五)UI布局—Layout

接下来,继续分析duilib之UI布局Layout,目前提供的布局有:VerticalLayout.HorizontalLayout.TileLayout.TabLayout.ChildLayout分别为垂直布局.水平布局.平铺布局.TAB布局.子窗体布局: 一般项目中用得比较多的是垂直布局.水平布局,我们将分别分析各布局: VerticalLayout:垂直布局,继承于CContainerUI容器UI类:而CContainerUI也继承于CControlUI,故VerticalLayout实际

Android仿微信UI布局视图(圆角布局的实现)

圆角按钮,或布局可以在xml文件中实现,但也可以使用图片直接达到所需的效果,以前版本的微信就使用了这种方法. 实现效果图:    不得不说,这种做法还是比较方便的. 源代码: MainActivity(没写任何代码,效果全在布局文件中实现): package com.android_settings; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity

Android性能调优篇之UI布局优化

详细内容请查看我的简书地址:Android性能调优篇之UI布局优化 或者我的个人博客地址:Android性能调优篇之UI布局优化

iOS开发>学无止境 - 遍历Model类的属性并完善使用Runtime给Model类赋值

在前几天的一篇博客<iOS开发之使用Runtime给Model类赋值>中介绍了如何使用运行时在实体类的基类中添加给实体类的属性赋值的方法,这个方法的前提是字典的Key必须和实体类的Property Name相同,然后通过运行时来生成和执行Setter方法给Model类的属性赋值. 通 过Runtime来给Model类属性赋值的好处是多多的,它便于代码的后期维护,并且提高了开发效率.当你拿到解析后的字典时你不用一个一个的通过 key去把字典的值赋值给相应的Model类的属性,本篇博客中会给出如何

React-Native入门指南(三) ——CSS和UI布局

React-Native入门指南 github:https://github.com/vczero/react-native-lession React-Native:用JavaScript开发你的原生应用,释放Native的UI体验,体验 Hybird开发效率. 最近一个星期写的文章如下: 第1篇hello react-native 第2篇认识代码结构 第3篇css和布局 第4篇学会react-native布局 第5篇ui组件 还有几篇需要这这周完成(这块会时刻更新): 第6篇JSX语法 第7

android UI布局

一.设置重复背景 在drawable文件夹下建一个mybackground.xml文件 在文件中写入: <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/mybg_img" android:tileMod

iOS8开发~UI布局(二)storyboard中autolayout和size class的使用详解

一.概要:前一篇初步的描述了size class的概念,那么实际中如何使用呢,下面两个问题是我们一定会遇到的: 1.Xcode6中增加了size class,在storyboard中如何使用? 2.auto layout该如何与size class配合来进行UI布局? 二.了解一件新事物的最好的办法就是实践,让我们揭开那神秘的面纱: 例子1.新建一个Single View Application template项目Demo1,拖拽一个newView到rootView上,并设置背景色为绿色, 然