我正在为营养跟踪应用程序构建并发症。我想使用提供多个较小的并发症,以便用户可以跟踪他们的营养。
例如:
通过这种方式在模块化表盘上,他们可以通过使用底部的三个“模块化小型”复杂功能来跟踪所有三个。
我知道这可以通过仅提供可以一次显示所有内容的更大尺寸来实现(例如“模块化大型”复杂功能),但我想为用户提供他们如何设置表盘的选择。
我看不到提供多个相同并发症的方法,有什么办法可以解决这个问题吗?
最佳答案
之前的答案已经过时了。 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 ?
第二步:
在
getCurrentTimelineEntries
和 getTimelineEntries
函数,然后我们可以使用 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/