widgetkit高级开发第2部分

介绍(Introduction)

This is the continuation of the WidgetKit: Advanced development - Part 1 article where we had a look at various advanced development techniques for the new iOS Widgets, here we will focus on Static and Dynamic Configuration of the widget and implement Widget User Interactions

这是WidgetKit的延续:高级开发-第1部分文章,我们了解了新的iOS Widget的各种高级开发技术,在这里,我们将重点介绍Widget的静态动态配置并实现Widget用户交互

小部件配置(Widget Configuration)

We have created nice widgets but, at the moment, we aren’t providing the user with a way to configure them, so let’s see how we can implement the configuration of the widget.Sticking to the Event app of the part 1 examples, we can imagine that our users might be interested in a certain type of events, so wouldn’t it be great to show just the events of that type? Maybe the users loves Movies or Travels, but they’re not really into Art or Music. In the previous article we were fetching all the events, now it’s time to allow the user to filter the events displayed in the widget in an easy way, just by holding a finger on the widget and selecting “Edit Widget”.

我们已经创建了不错的窗口小部件,但是目前我们还没有为用户提供配置窗口小部件的方法,因此让我们看一下如何实现窗口小部件的配置。在第1部分示例的Event应用程序中,我们可以想象我们的用户可能对某种类型的事件感兴趣,因此仅显示该类型的事件不是很好吗? 也许用户喜欢电影或旅行,但他们并不真正喜欢艺术或音乐。 在上一篇文章中,我们获取了所有事件,现在是时候让用户通过简单的方式过滤窗口小部件中显示的事件,只需用手指在窗口小部件上并选择“编辑窗口小部件”即可。

WidgetKit allow us at do that in 2 ways, the first is a simple Static Configuration, the second one is a more complex Dynamic Configuration, let’s have a look at them.

WidgetKit允许我们以2种方式进行操作,第一种是简单的“静态配置”,第二种是更复杂的“动态配置”,让我们来看看它们。

静态小部件配置 (Static Widget Configuration)

The static widget configuration is quite easy and simple. First, for static configuration I mean “hard-coded” configuration where we provide a configurable parameter and the associated values that can take. Any widget can be configured through parameters of various types, and each widget will save its own configuration state for itself. This means a user can have 4 small size widgets of the same app on their home screen, with each one configured differently.

静态窗口小部件配置非常简单。 首先,对于静态配置,我的意思是“硬编码”配置,其中我们提供了一个可配置的参数以及可以采用的关联值。 可以通过各种类型的参数来配置任何窗口小部件,并且每个窗口小部件将为其自身保存其自己的配置状态。 这意味着用户可以在其主屏幕上拥有4个相同应用程序的小尺寸小部件,每个小部件的配置均不同。

The easiest way to have a configurable widget is to be sure that Include Configuration Intent is checked when you add the Widget Extension to your project. When you do so, you will notice some differences in respect to a Widget Extension that does not include it:

拥有可配置窗口小部件的最简单方法是,在将窗口小部件扩展添加到项目中时,确保已选中“包括配置意图” 。 这样做时,您会注意到在不包含它的Widget扩展方面存在一些差异:

  • Provider class of the widget is no longer a TimelineProvider but an IntentTimelineProvider, and this also affects the getSnapshot and getTimeline functions, where we see one more parameter added at the beginning of the input declaration, the configuration parameter.

    小部件的Provider类不再是TimelineProvider而是IntentTimelineProvider ,这也会影响getSnapshotgetTimeline函数,在输入声明的开头我们看到了另一个参数,即配置参数。

  • TimelineEntry class now has a shiny new configuration property

    现在, TimelineEntry类具有闪亮的新配置属性

  • A new file has been created in your widget folder with the .intentdefinition file extension

    在您的小部件文件夹中创建了一个新文件,扩展名为.intentdefinition

The new file is the important one, when you select it, the Intent editor is opened.

新文件很重要,选择该文件后,将打开Intent编辑器。

NOTE: Intents have been available since iOS 10.0, but have mostly been used for interactions with Siri. It’s a quite a big topic, but you can easily find various articles about that. For our purpose there is no need to know too much about this topic, so don’t be afraid if you have never used Intents.

注意: Intent自iOS 10.0起可用,但主要用于与Siri进行交互。 这是一个很大的话题,但是您可以轻松找到有关此的各种文章。 出于我们的目的,您无需对此主题了解太多,因此,如果您从未使用过Intent,请不要担心。

In the Intent Editor you will see a lot of parameters and checkboxes, but the only one we want to be sure is checked for now is Intent is eligible for widgets:

在Intent编辑器中,您将看到很多参数和复选框,但是我们现在要确定唯一要检查的参数是Intent是否适用于小部件:

Now, let’s add a configuration, hit the + button below the list of Parameters. Adding a parameter and selecting it in the list will show you a lot of settings applied to that parameter. Each parameter can have its own type: String, Booleans, Enums or even new types (we will see this one in the dynamic configuration paragraph).

现在,让我们添加一个配置,点击“参数”列表下方的+按钮。 添加参数并在列表中选择它会显示出许多应用于该参数的设置。 每个参数可以具有自己的类型:字符串,布尔值,枚举甚至是新类型(我们将在动态配置段落中看到这一类型)。

As mentioned, we want to let the user filter the events shown by category, so, first, we add a paramete

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值