UIKIt学习笔记—App and Environment部分(三)

太难了,我的实习不知道还能不能去啊,我想回北京555~

今天是2021年12月30号
今天主要讲

目录

引言

UIKit用UIWindowScene对象管理每个app的UI的实例,窗口包含了显示UI实例的window和VC,每个窗口都有一个相关的UIWindowSceneDelegate对象,用来协调app数据和UIKit的交互,所有窗口并行执行,共享内存和进程资源和空间,一个app可同时拥有多个窗口和窗口代理对象。可以通过 UIApplicationDelegate 对象为app配置新窗口
UIKIt学习笔记—App and Environment部分(三)

Preparing Your UI to Run in the Foreground

app响应至前台一般由用户操作引起,前台转换一般用于更新UI,请求资源,响应用户请求
UIKit中所有的状态转换结果都会发送到合适的代理对象中去
iOS13之后— UISceneDelegate对象
iOS12之前—UIApplicationDelegate 对象
You can support both types of delegate objects, but UIKit always uses scene delegate objects when they are available. UIKit notifies only the scene delegate associated with the specific scene that is entering the foreground.
两句废话

At launch time, the system starts your app in the inactive state before transitioning it to the foreground. Use your app’s launch-time methods to perform any work needed at that time. For an app that is in the background, UIKit moves your app to the inactive state by calling one of the following methods:
For apps that support scenes—The sceneWillEnterForeground(_ : ) method of the appropriate scene delegate object.
For all other apps—The applicationWillEnterForeground(: ) method.
When transitioning from the background to the foreground, use these methods to load resources from disk and fetch data from the network.
启动时,系统会在app进入前台之前设置app为不活跃状态,用启动时的方法来执行一些必要操作,对于后台应用,UIKit将会调用以下方法设置他们为不活跃状态:
支持多窗口的:相应窗口代理对象的sceneWillEnterForeground(: ) 方法
其他应用: applicationWillEnterForeground(_: ) 方法

The system moves your app to the active state immediately before displaying the app’s UI. Activation is a good time to configure your app’s UI and runtime behavior; specifically:

  • Show your app’s windows, if needed.
  • Change the currently visible view controller, if needed.
  • Update the data values and state of views and controls.
  • Display controls to resume a paused game.
  • Start or resume any dispatch queues that you use to execute tasks.
  • Update data source objects.
  • Start timers for periodic tasks.
  • Put your configuration code in one of the following methods:
  • For a scene-based UI—The sceneDidBecomeActive(_: ) method of the
    appropriate scene delegate object.
  • For all other apps—The applicationDidBecomeActive(_: ) method of your app delegate object.

Activation is also the time to put finishing touches on your UI before displaying it to the user. Don’t run any code that might block your activation method. Instead, make sure you have everything you need in advance. For example, if your data changes frequently outside of the app, use background tasks to fetch updates from the network before your app returns to the foreground. Otherwise, be prepared to display existing data while you fetch changes asynchronously.

进入前台时会激活app使其进入活跃态,此时可以执行以下操作

  • 显示window
  • 更改可视的VC
  • 更新数据和 视图状态
  • 显示被暂停的游戏的开始界面
  • 开始或暂停所有执行任务的派发队列
  • 更新数据资源对象
  • 启动周期任务的计时器
  • 将配置代码放到以下方法中
  • 基于窗口的—相应窗口代理对象的 sceneDidBecomeActive(_: ) 方法
  • 其他app—app代理对象的 applicationDidBecomeActive(_: ) 方法
    不要写那种可能堵塞激活的代码

When your activation method returns, UIKit shows any windows that you made visible. It also notifies any relevant view controllers that their views are about to appear. Use your view controller’s viewWillAppear(_: ) method to perform any final updates to your interface. For example:

  • Start user interface animations, as appropriate.
  • Begin playing media files, if auto-play is enabled.
  • Begin displaying graphics for games and immersive content at their
    full frame rates.

Don’t try to show a different view controller or make major changes to your user interface. By the time your view controller appears onscreen, your interface should be ready to display.
当激活结束,UIKit将会显示所有可视的window,通知所有相关VC他们的View将要显示,用VC的viewWillAppear(_: ) 方法执行所有UI的最后更新,比如:

  • 启动界面动画
  • 播放音视频
  • 满帧率显示游戏图像
    不要妄想显示一个不同的VC或对UI大改

Preparing Your UI to Run in the Background

程序进入后台有很多情况,当进入后台后,当用户退出前台应用,系统就会把app设置为后台状态,有时也可能直接把app启动到后台,或者把一个挂起的应用移到后台,给他CPU时间让他执行一些任务
后台应用应尽量少的执行任务

If your app was previously in the foreground, use the background transition to stop tasks and release any shared resources. If your app enters the background to process an important event, process the event and exit as quickly as possible.
All state transitions result in UIKit sending notifications to the appropriate delegate object:
In iOS 13 and later—A UISceneDelegate object.
In iOS 12 and earlier—The UIApplicationDelegate object.
You can support both types of delegate objects, but UIKit always uses scene delegate objects when they are available. UIKit notifies only the scene delegate associated with the specific scene that is entering the background.
如果您的应用之前在前台,请使用后台过渡停止任务并释放所有共享资源,如果app在后台执行重要任务,那么请尽快让他执行完并结束它

The system deactivates apps for several reasons. When the user exits the foreground app, the system deactivates that app immediately before moving it to the background. The system also deactivates apps when it needs to interrupt them temporarily—for example, to display system alerts. In the case of a system panel, the system reactivates the app when the user dismisses the panel.
During deactivation, UIKit calls one of the following methods of your app:
For apps that support scenes—The sceneWillResignActive(: ) method of the appropriate scene delegate object.
For all other apps—The applicationWillResignActive(
: ) method of the app delegate object.
Use deactivation to preserve the user’s data and put your app in a quiet state by pausing all major work; specifically:
Save user data to disk and close any open files.
Suspend dispatch and operation queues.
Don’t schedule any new tasks for execution.
Invalidate any active timers.
Pause gameplay automatically.
Don’t commit any new Metal work to be processed.
Don’t commit any new OpenGL commands.
系统出于几个原因停用app。当app退出前台时,系统会在将该app移到后台之前立即停用该app。系统还会在需要暂时中断app时停用app,例如显示系统警报。对于系统面板,当用户关闭面板时,系统会重新激活应用程序。
停用期间,UIKit会调用以下方法

  • 基于窗口的—相应窗口代理对象的 sceneWillResignActive(_: ) 方法
  • 其他app—app代理对象的 applicationWillResignActive(_: ) 方法
    使用停用来保护数据并暂停所有主要工作使app进入一种安静的状态
  • 将用户数据保存到磁盘并锁住文件
  • 中断多线程任务
  • 不要规划新的任务
  • 暂停所有计时器
  • 自动暂停游戏
  • Metal和OpenGL也停了
上一篇:血色先锋队


下一篇:自监督-How to find your friendly neighborhood: Graph Attention Design with Self-Supervision