watchos 的多种复杂功能

Multiple complications in watchos

我正在为一个营养追踪应用构建并发症。我想使用提供多个较小的并发症,以便用户可以跟踪他们的营养。

EG:

这样在模块化表盘上,他们可以通过使用三个底部 'modular small' 复杂功能来跟踪所有三个。

我知道这可以通过仅提供可以同时显示所有内容的更大尺寸来实现(例如 'modular large' 复杂功能),但我想让用户选择他们如何设置他们的表盘。

我看不出有什么方法可以提供多个相同的并发症,有什么办法解决这个问题吗?

目前无法在同一个系列中创建多个复杂功能(例如模块化小型、模块化大型、实用小型等)。

您可以为用户提供一种自定义每个并发症以显示碳水化合物、蛋白质和脂肪的方法。您甚至可以为每个复杂功能系列显示不同的数据,但就具有显示不同数据的 2 个模块化小型复杂功能而言,目前还不可能。

如果您将 2 个相同的内置 Apple 复杂功能放在表盘的不同位置,它们会显示相同的内容,您就会看到这一点。如果 Apple 连自己的并发症都没有做到,那么这很可能是不可能的。希望解释有所帮助。

之前的回答已经过时了。 WatchOS 7 及更高版本,我们现在可以为我们的应用向同一个复杂功能系列添加多个复杂功能。

第 1 步:

在我们的 ComplicationController.swift 文件中,我们可以使用 getComplicationDescriptors 函数,它允许我们描述我们在应用程序中提供的复杂功能。

descriptors 数组中,我们可以为我们想要构建的每个系列的每种复杂功能追加一个 CLKComplicationDescriptor()

func getComplicationDescriptors(
  handler: @escaping ([CLKComplicationDescriptor]) -> Void) {
  var descriptors: [CLKComplicationDescriptor] = []
  for progressType in dataController.getProgressTypes() {
    var dataDict = Dictionary<AnyHashable, Any>()
    dataDict = ["id": progressType.id]
    
    // userInfo helps us know which type of complication was interacted with by the user
    let userActivity = NSUserActivity(
       activityType: "org.example.foo")
    userActivity.userInfo = dataDict

    descriptors.append(
       CLKComplicationDescriptor(
         identifier: "\(progressType.id)", 
         displayName: "\(progressType.title)",
         supportedFamilies: CLKComplicationFamily.allCases, // you can replace CLKComplicationFamily.allCases with an array of complication families you wish to support
         userActivity: userActivity)
    )
  }
  handler(descriptors)
}

该应用程序现在将为您支持的每个复杂功能系列提供多个复杂功能(等于 dataController.getProgressTypes() 数组的长度)。

但是现在如何针对不同的并发症显示不同的数据和视图?

第 2 步:

getCurrentTimelineEntriesgetTimelineEntries 函数中,我们可以使用 complication.identifier 值来识别调用此复杂项时传递的数据。

例如,在 getTimelineEntries 函数中:

 func getTimelineEntries(for complication: CLKComplication, after date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
    // Call the handler with the timeline entries after the given date
    var entries: [CLKComplicationTimelineEntry] = []
    
    ...
    ...
    ...
    
    var next: ProgressDetails
    // Find the progressType to show using the complication identifier
    if let progressType = dataController.getProgressAt(date: current).first(where: {[=11=].id == complication.identifier}) {
      next = progressType
    } else {
      next = dataController.getProgressAt(date: current)[0] // Default to the first progressType
    }
     
    let template = makeTemplate(for: next, complication: complication)
    let entry = CLKComplicationTimelineEntry(
        date: current,
        complicationTemplate: template)
    entries.append(entry)

    ...
    ...
    ...

    handler(entries)
  }

您可以类似地找到在 getCurrentTimelineEntrygetLocalizableSampleTemplate 函数中传递的数据。

第 3 步:

尽情享受吧!