ios小部件_带WidgetKit的iOS 14中的小部件

ios小部件

Note: This article is a short text summary of the WWDC20 session.

注意:本文是 WWDC20会议 的简短摘要

WidgetKit is a new Swift framework introduced in WWDC20 that allows you to bring out the most important data from your app on your home screen!

WidgetKit是WWDC20中引入的新Swift框架,可让您在主屏幕上从应用程序中提取最重要的数据!

设计 (Design)

Widgets in iOS need to be designed keeping three things in mind: glanceability, relevance, and personalisation.

iOS的小部件需要在设计时牢记三件事:浏览性,相关性和个性化。

Image for post
Apple Developer. Apple Developer提供的照片。
  • Glanceability — Data should be presented in a meaningful way.

    可浏览性-数据应以有意义的方式呈现。
  • Relevance — Based on usage patterns, iOS14 automatically shows relevant widgets. You do have some level of control in helping iOS understand how frequently users would want to see your widget, especially when multiple widgets are in the same smart stack.

    相关性-iOS14根据使用模式自动显示相关小部件。 您确实具有某种程度的控制权,可以帮助iOS了解用户希望多久查看一次您的小部件,尤其是当多个小部件位于同一智能堆栈中时。
  • Personalisation — Shown data can be resized and personalised based on the user’s preferences (e.g. a weather widget could show the weather for the user’s geographical location or a calendar widget could show events based on the current date and time).

    个性化-可以根据用户的喜好调整显示数据的大小和个性化(例如,天气小部件可以显示用户地理位置的天气,或者日历小部件可以根据当前日期和时间显示事件)。
Image for post
Apple Developer. Apple Developer提供的照片。

性能 (Performance)

Widgets are placed on the home screen, so they need to load fast. Hence, rather than being mini-apps, widgets are just views that are provided by an app extension for a particular time beforehand. This is done with WidgetKit extensions (Swift background extensions) by returning a bunch of views in a timeline and sending them to the widget on the home screen, which will present the appropriate view based on the current time.

小部件放置在主屏幕上,因此需要快速加载。 因此,小部件不是迷你应用程序,而只是应用程序扩展程序在特定时间之前提供的视图。 这是通过WidgetKit扩展(Swift背景扩展)来完成的,方法是在时间轴中返回一堆视图并将其发送到主屏幕上的小部件,该小部件将基于当前时间显示适当的视图。

Image for post
Apple Developer. Apple Developer提供的照片。

The timeline can be refreshed from the main app. A timeline is a combination of views and dates that denote at what time the particular view is to be shown. Typically, days worth of content needs to be returned from the extension, which is then serialized to disk and one of the views is shown based on the current time. However, for widgets where up-to-date information is to be returned, reloads are used. The system wakes up the extension and asks for a timeline for each widget placed on the device. ReloadPolicy can be defined by your app so that system knows when to ask for the next timeline. Configuration options include atEnd of the existing timeline, after(date: Date), and never.

时间线可以从主应用程序刷新。 时间轴是视图和日期的组合,表示在什么时间显示特定视图。 通常,需要从扩展中返回几天的内容,然后将该扩展序列化到磁盘上,并根据当前时间显示其中一个视图。 但是,对于要返回最新信息的小部件,将使用重新加载。 系统唤醒扩展程序,并要求为设备上放置的每个小部件提供时间线。 ReloadPolicy可以由您的应用定义,以便系统知道何时要求下一个时间表。 配置选项包括现有时间轴的atEndafter(date: Date)never

Widget content can also be updated when a background notification is received or when a user makes changes to the app. In case of background notifications or user edits to your app, you can use the Swift-based WidgetKit API via WidgetCenter to reload the timeline, thus waking the extension. However Widgets are not meant to be updated every second, even if you set reload policy that way, iOS will limit updates based on user’s interaction with the widget.

当收到后台通知或用户对应用程序进行更改时,也可以更新窗口小部件内容。 如果有后台通知或用户对您的应用程序进行编辑,则可以通过WidgetCenter使用基于Swift的WidgetKit API重新加载时间线,从而唤醒扩展程序。 但是,即使您以这种方式设置重载策略,也不意味着小部件会每秒更新一次,iOS会根据用户与小部件的交互来限制更新。

Widgets are not mini-apps. They just show content from your app on the home screen. Widgets can come in three different sizes: small square, rectangle, and large square. It’s not necessary to design for all sizes, but the more, the better.

小部件不是迷你应用程序。 它们只是在主屏幕上显示您的应用程序中的内容。 小部件可以有三种不同的大小:小正方形,矩形和大正方形。 不必针对所有大小进行设计,但是越多越好。

Widget configuration options are built using intents. Configuration UI is automatically generated using intents.

窗口小部件配置选项是使用意图构建的。 配置UI是使用意图自动生成的。

Widgets are built entirely with SwiftUI. Though SwiftUI is supported since iOS 13, widgets are supported since iOS 14.

小部件完全由SwiftUI构建。 尽管自iOS 13开始支持SwiftUI,但自iOS 14开始支持小部件。

Smart stacks shown on the home screen are just a stack of widgets. The widget that appears on top is decided by on-device intelligence but can be controlled with a Siri shortcuts donation and the WidgetKit API (to help the system decide which widget would be relevant).

主屏幕上显示的智能堆栈只是小部件的堆栈。 顶部显示的小部件由设备上的智能决定,但可以通过Siri快捷方式捐赠和WidgetKit API(以帮助系统确定哪个小部件相关)进行控制。

定义小部件 (Defining a Widget)

  • kind — A single extension to support multiple types of widgets by defining their configuration.

    kind —一种扩展,通过定义它们的配置来支持多种类型的窗口小部件。
  • configuration — Intent types

    配置—意向类型
  • supportedFamilies — Widget size

    supportedFamilies —小部件大小

  • placeholder — Default content

    占位符-默认内容
placeholder data shown in iOS14 widget when there is no data to display
Akashlal Akashlal

kind: StaticConfiguration, IntentConfiguration

种类: StaticConfigurationIntentConfiguration

supportedFamilies: systemSmall, systemMedium, systemLarge.

支持的家庭: systemSmallsystemMediumsystemLarge

placeholder: Represents the content of the widget. It should not have any user data. This UI is retrieved only sparingly, especially when there is no data to display at all.

占位符:代表小部件的内容。 它不应包含任何用户数据。 仅少量检索该UI,尤其是在根本没有数据可显示时。

The entire widget can be associated with a URL link using the widgetURL API. While systemSmall can be associated with only one link, sublinks can be added to systemMedium and systemLarge using the new Link API in SwiftUI.

可以使用widgetURL API将整个窗口小部件与URL链接相关联。 虽然systemSmall只能与一个链接关联,但是可以使用systemLarge中的新Link API将子链接添加到systemMedium和systemLarge。

最后的想法 (Final Thoughts)

Think of widgets as a part of your app displayed on the home screen. With beautiful rounded edges, you can show glanceable data from your app with deep links to directly open that particular page when the user taps on the widget. Built with SwiftUI, widgets can support dark mode and are of dynamic type, ensuring accessibility to those preferring large fonts.

将小部件视为显示在主屏幕上的应用程序的一部分。 通过优美的圆角边缘,您可以显示带有深层链接的应用程序中的数据一览无余,从而可以在用户点击小部件时直接打开特定页面。 使用SwiftUI构建的小部件可以支持暗模式,并且是动态类型的,从而确保了那些喜欢大字体的人的可访问性。

Ready to build your first widget? I wrote another piece on building your first widget with WidgetKit from scratch to a working prototype, click the link to check it out!

准备构建您的第一个小部件了吗? 我写了另一篇关于使用WidgetKit从头构建第一个小部件到工作原型的文章,单击链接进行检查!

翻译自: https://medium.com/better-programming/widgetkit-in-ios-14-wwdc20-81cf10f51af9

ios小部件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值