所以我尝试用Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in每秒更新一次headerTextProvider:
我猜这不是做这件事的正确方式?
// MY STUFF HERE
//----------------------------------------------------------------------------------------------------
func getCurrentTimelineEntry(for complication: CLKComplication,
withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
@ObservedObject var service = Service()
print("-------------------->>>>>>>>>> \(service.currentTime)")
let date = Date()
//let currentTime = service.currentTime
let title = service.title
let time = service.time
var template: CLKComplicationTemplate!
let line1 = CLKSimpleTextProvider(text:title)
let line2 = CLKSimpleTextProvider(text:time)
//let line3 = CLKSimpleTextProvider(text:currentTime)
if complication.family == .modularLarge {
template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: line1, body1TextProvider: line2)
let entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)
handler(entry)
}
else{
handler(nil)
}
}完整的代码
//
// ComplicationController.swift
// WatchApp WatchKit Extension
//
// Created by Mathias Halén on 2021-06-22.
// Copyright © 2021 Mathias Halén. All rights reserved.
//
import ClockKit
import SwiftUI
class ComplicationController: NSObject, CLKComplicationDataSource {
final class Service: ObservableObject{
static let sharedInstance1 = Service()
@Published var currentTime = " first time"
@Published var title = "Hello"
@Published var time = "World"
init() {
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
print("----------> WatchOS -> ComplicationController \(Date())")
self.currentTime = GlobalVaribles.sharedInstance.currentTime
self.title = GlobalVaribles.sharedInstance.title
self.time = GlobalVaribles.sharedInstance.time
}
}
}
// MARK: - Complication Configuration
func getComplicationDescriptors(handler: @escaping ([CLKComplicationDescriptor]) -> Void) {
let descriptors = [
CLKComplicationDescriptor(identifier: "complication",
displayName: "Test 2",
supportedFamilies: CLKComplicationFamily.allCases)
// Multiple complication support can be added here with more descriptors
]
// Call the handler with the currently supported complication descriptors
handler(descriptors)
}
func handleSharedComplicationDescriptors(_ complicationDescriptors: [CLKComplicationDescriptor]) {
// Do any necessary work to support these newly shared complication descriptors
}
// MARK: - Timeline Configuration
func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
// Call the handler with the last entry date you can currently provide or nil if you can't support future timelines
let currentDate = Date()
handler(currentDate)
}
func getPrivacyBehavior(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
// Call the handler with your desired behavior when the device is locked
handler(.showOnLockScreen)
}
func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
// This method will be called once per supported complication, and the results will be cached
handler(nil)
}
// MY STUFF HERE
//----------------------------------------------------------------------------------------------------
func getCurrentTimelineEntry(for complication: CLKComplication,
withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
@ObservedObject var service = Service()
print("-------------------->>>>>>>>>> \(service.currentTime)")
let date = Date()
//let currentTime = service.currentTime
let title = service.title
let time = service.time
var template: CLKComplicationTemplate!
let line1 = CLKSimpleTextProvider(text:title)
let line2 = CLKSimpleTextProvider(text:time)
//let line3 = CLKSimpleTextProvider(text:currentTime)
if complication.family == .modularLarge {
template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: line1, body1TextProvider: line2)
let entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)
handler(entry)
}
else{
handler(nil)
}
}
}发布于 2021-06-24 11:49:57
通常,您不会将时间线条目“推”到您的复杂性中。您可以在ClockKit请求时提供时间轴条目。
您可以使用reloadTimeline和extendTimeline来触发来自ClockKit的请求,但通常只会定期使用;例如,当用户交互导致您的应用程序加载新数据时,或者可能基于计划的刷新任务。
有关更多详细信息,请参阅Keeping Your Complications Up to Date。
使用Timer并不是一种真正有意义的方法;Timer只有在手表应用程序处于前台时才会触发,如果它处于前台,则不会显示您的复杂性。
您可以做的是在您的数据源中实现getTimelineEndDate和getTimelineEntries;然后,当您的数据源被要求提供条目时,您可以提供将来的条目,以便更复杂地显示。
首先,为结束日期提供.distantFuture,这表明您的复杂性可以无限期地提供数据。
func getTimelineEndDate(for complication: CLKComplication,
withHandler handler: @escaping (Date?) -> Void) {
handler(.distantFuture)
}其次,提供一系列时间线条目,例如,在接下来的3600秒中,每个条目一个。
func getTimelineEntries(for complication: CLKComplication,
after date: Date,
limit: Int,
withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
guard complication.family == .modularLarge else {
handler(nil)
return
}
let now = Date()
var entries = [CLKComplicationTimelineEntry]()
let line1 = CLKSimpleTextProvider(text:title)
for i in 0..<3600 {
let template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: line1, body1TextProvider: line2)
let line2 = CLKSimpleTextProvider(text:"\(i)")
let entry = CLKComplicationTimelineEntry(date: date.addingTimeInterval(Double(i)), complicationTemplate: template)
entries.append(entry)
}
completion(entries)
}https://stackoverflow.com/questions/68106565
复制相似问题