美文网首页
Widget 学习 IntentTimeLineProvider

Widget 学习 IntentTimeLineProvider

作者: 弑神指 | 来源:发表于2020-11-10 09:22 被阅读0次

    IntentTimeLineProvider (意图时间表提供商)

    建议WidgetKit何时更新用户可配置的窗口小部件显示的类型

    宣言

    protocol IntentTimeLineProvider
    

    总览

    *指控时间轴提供程懋序执行与相他为的功能,但还将用户配置的详细信息合并到时间轴条目(。)TimelineProvider

    例如,在显示用户已选择的游戏角色的健康状态的小部件中,提供者会收到一个自定义意图,用于指定要显示的角色。然后,在Xcode项目中,定义一个自定义SiriKit Intent Definition文件。意向定义可以包括角色的详细信息,例如角色的名称,头像,战略联盟等。

    IntentTimelineProvider-IntentDefinition@2x.png

    Xcode生成以下INIntent定制意图:

    public class SelectCharacterIntent: INIntent {
        @NSManaged public var characterName: String?
        @NSManaged public var avatar: String?
        @NSManaged public var alliances: [String]?
        @NSManaged public var healthLevel: NSNumber?
    }
    

    因为用户可以添加特定小部件的多个实例,所以您的提供者需要一种方法来区分WidgetKit询问的是哪个实例。当WidgetKit调用getSnapshot(for:in:completion:)或getTimeline(for:in:completion:)时,它会传递一个自定义INIntent的实例,该实例由用户选择的详细信息配置。游戏小部件提供程序访问意图的属性,并将它们包含在TimelineEntry中。然后,WidgetKit调用小部件配置的内容闭包,传递时间轴条目以允许视图访问用户配置的属性。例如,提供者可能实现一个TimelineEntry,它的属性与自定义意图中的属性相对应:

    struct CharacterDetailEntry : TimeLineEntry {
      var date:Date
      var name:String?
     var avator:String?
     var alliances:[String]?
     var healthLevel:Double?
    }
    

    为了生成快照,游戏小部件提供程序使用意图中的属性初始化角色详细信息条目。

     struct  CharacterDetailProvider:IntentTimeLineProvider {
        func getSnapshot(for configuration:SelectCharacterIntent,in context:Context,completion:@escaping (CharacterDetailEntry) ->Void) {
      let entry = CharacterDetailEntry(
            date:Date(),
           name:configuration.characterName,
          avatar:configuration.avatar,
          alliances:configuration.alliances,
         healthLevel:configuration.healthLevel?.doubleValue
      )
     completion(entry)
    }
    

    话题

    产生时间表

    func getSnapshot(for: Self.Intent, in: Self.Context, completion: (Self.Entry) -> Void)

    提供一个时间轴条目,表示小部件的当前时间和状态。
    需要。 提供了默认实现。

    func getTimeline(for: Self.Intent, in: Self.Context, completion: (Timeline<Self.Entry>) -> Void)

    提供当前时间以及(可选)任何将来时间的时间线条目数组,以更新窗口小部件。
    需要。 提供了默认实现。

    func placeholder(in: Self.Context) -> Self.Entry

    需要。 提供了默认实现。

    associatedtype Entry : TimelineEntry

    需要。

    associatedtype Intent : INIntent

    需要。

    实例方法

    func snapshot(for: Self.Intent, with: Self.Context, completion: (Self.Entry) -> ())

    需要。 提供了默认实现。

    func timeline(for: Self.Intent, with: Self.Context, completion: (Timeline<Self.Entry>) -> ())

    需要。 提供了默认实现。

    另外阅读

    相关文件

    class INIntent

    在您的应用或Intents扩展中实现的请求。

    时间表管理

    使小部件保持最新

    计划小部件的时间轴,以使用动态视图及时显示相关信息,并在发生变化时更新时间轴。

    protocol TimelineProvider

    建议WidgetKit何时更新窗口小部件显示的类型。

    struct TimelineProviderContext

    一个对象,其中包含有关如何渲染小部件的详细信息,包括其大小以及它是否出现在小部件库中。

    protocol TimelineEntry

    一种类型,指定显示小部件的日期,并可选地指示小部件内容的当前相关性。

    struct Timeline

    一个对象,指定WidgetKit更新窗口小部件视图的日期。

    class WidgetCenter

    包含用户配置的小部件列表的对象,用于重新加载小部件时间线。

    相关文章

      网友评论

          本文标题:Widget 学习 IntentTimeLineProvider

          本文链接:https://www.haomeiwen.com/subject/afwfbktx.html