简体   繁体   English

如何检测 SwiftUI 和 UIKit 中的动态岛大小和变化

[英]How to detect dynamic-island size and changes in SwiftUI & UIKit

How can we detect the size and changes of the iPhone 14 pro dynamic-island ?如何检测iPhone 14 pro动感岛的大小和变化?

Since the safe area provides a safe rectangle inside the rounded screen and top or bottom insets, but nothing about an object inside the rectangle.由于安全区域在圆形屏幕和顶部或底部插图内提供了一个安全矩形,但矩形没有任何关于 object 的信息。

The all we know about safe area:我们对安全区的了解:

在此处输入图像描述

We don't know yet because Apple has not released any APIs for the Dynamic Island yet.我们还不知道,因为 Apple 还没有为 Dynamic Island 发布任何 API。 The current Xcode 14 Release Candidate does not include related APIs.当前的Xcode 14 Release Candidate不包含相关 API。

Apple engineers in the developer forum have explained that this is coming to iOS 16 and Xcode "later this year".开发者论坛中的苹果工程师解释说,这将在“今年晚些时候”发布到 iOS 16 和 Xcode。

First of all for most developers it is sufficient to use safeArea as we did before, because dynamic island does not cover it (if not in expanded state).首先,对于大多数开发人员来说,像我们之前那样使用safeArea就足够了,因为动态岛不会覆盖它(如果不处于展开状态)。 There are currently 3 states of fynamic island: fynamic island目前有3种状态:

  1. Compact.袖珍的。 In the Dynamic Island, the system uses the compact presentation when there's only one Live Activity that's currently active.在 Dynamic Island 中,当当前只有一个 Live Activity 处于活动状态时,系统会使用紧凑的呈现方式。 紧凑状态
  2. Minimal .最小的。 When multiple Live Activities are active, the system uses the minimal presentation to display two of them in the Dynamic Island.当多个 Live Activity 处于活动状态时,系统会使用最小呈现方式在 Dynamic Island 中显示其中两个。 One Live Activity appears attached to the Dynamic Island while the other appears detached.一个 Live Activity 显示为附加到 Dynamic Island,而另一个显示为分离。

最小的

  1. Expanded .扩大了 When people touch and hold a Live Activity in a compact or minimal presentation, the system displays the content in an expanded presentation.当人们触摸并按住紧凑或最小演示中的实时活动时,系统会以扩展演示的形式显示内容。

在此处输入图像描述

You can check each state if you use dynamic island yourself as part WidgetKit.如果你自己使用动态岛作为 WidgetKit 的一部分,你可以检查每个 state。 Create a new widget extension if your app doesn't already include one.如果您的应用程序还没有包含一个新的小部件扩展,请创建一个新的小部件扩展。 Live Activities use WidgetKit functionality and SwiftUI for their user interface.实时活动使用WidgetKit功能和SwiftUI作为其用户界面。 For creating new Activities, please refer to Apple official documentation for Displaying live data with Live Activities .新建Activity请参考苹果官方文档Displaying live data with Live Activities If you are not intending to use Activities, but want to somehow be aware of dynamic island available sizes, Apple provides as with Dynamic island size sheet included in Live Activities documentation :如果您不打算使用 Activities,但想以某种方式了解动态岛的可用大小,Apple 会提供Live Activities 文档中包含的动态岛尺寸表:

动态岛尺寸表

Here is an attachment of how safeArea looks implemented with dynamic island:这是使用动态岛实现 safeArea 的附件:

安全有动态岛

It is not official, but according to unofficial rumours the dynamic island API will be part of Activities and ActivityKit and would be available later this year of 2022 (exact date not specified ).这不是官方的,但根据非官方的传言,动态岛 API 将成为活动和 ActivityKit 的一部分,并将于 2022 年晚些时候上市(具体日期未指定)。 For further information pay close look for new Beta version of Activities如需更多信息,请密切关注活动的新 Beta 版

You can simply try to get the safe area top insets and use the dynamic height as you want.您可以简单地尝试获取安全区域顶部插图并根据需要使用动态高度。

In my scenario, requirement was to change status bar background colour.在我的场景中,要求是更改状态栏背景颜色。 So I did something like this:所以我做了这样的事情:

    let window = UIApplication.shared.windows.first
    let topPadding = window?.safeAreaInsets.top
    let statusBar = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: topPadding ?? 0.0))
    statusBar.backgroundColor = UIColor(named: "AppPrimaryColor")
    UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.addSubview(statusBar)

Dynamic island is fun to play with.动态岛屿很有趣。 screen shot截屏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM