watchkit - watchos 中的多种并发症

标签 watchkit apple-watch

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

例如:

  • 'MyApp - 碳水化合物'
  • 'MyApp - 蛋白质'
  • 'MyApp - 胖子'

  • 通过这种方式在模块化表盘上,他们可以通过使用底部的三个“模块化小型”复杂功能来跟踪所有三个。

    我知道这可以通过仅提供可以一次显示所有内容的更大尺寸来实现(例如“模块化大型”复杂功能),但我想为用户提供他们如何设置表盘的选择。

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

    最佳答案

    之前的答案已经过时了。 WatchOS 7 以上 ,我们现在可以为我们的应用程序添加多个并发症到同一个并发症系列。
    第一步:
    在我们的 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() 数组的长度)。
    但是您现在如何针对不同的并发症显示不同的数据和 View ?
    第二步:
    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: {$0.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)
      }
    
    您可以类似地找到 getCurrentTimelineEntry 中传递的数据。和 getLocalizableSampleTemplate职能。
    第三步:
    享受!

    关于watchkit - watchos 中的多种并发症,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48531764/

    相关文章:

    nstimer - Apple Watch 显示器 sleep 暂停 NSTimer,但 WKInterfaceTimer 一直倒计时

    xcode - 在 Apple Watch 模拟器(xCode 8、Swift 3、iOS 10)上的 watchOS 中运行 SpriteKit 游戏 - libswiftSwiftOnoneSupport 错误

    swiftui - 如何在 Apple Watch 的扩展委托(delegate)中访问 SwiftUI 内容 View ?

    ios - 创建通知场景后如何将它添加到 WatchKit 目标?

    ios - 基于动态页面的 Apple Watch 应用

    swift 用户默认 :sharing data via share group doesn't work

    iOS 应用程序在与 Watch 通信 1 分钟后进入休眠状态 - WatchKit

    swift - 我可以以编程方式创建 WKInterfaceButton 吗?

    ios - 如果模型对象文件有很多依赖项,如何将其导入到我的 watch 扩展中

    ios - 如何测量 Apple Watch 和 iPhone 之间的距离?