我正在尝试向Watch应用程序(使用Swift UI构建)添加复杂性,但是当我在iPhone的Watch应用程序中选择“并发症”时,Watch应用程序尚未显示任何复杂性。
我创建了一个“ ComplicationController”,并为“ CircularSmall”和“ ModularLarge”并发症添加了必要的代码。下面是代码
class ComplicationController: NSObject, CLKComplicationDataSource {
func getSupportedTimeTravelDirections(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimeTravelDirections) -> Void) {
handler([])
}
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
if complication.family == .circularSmall
{
let template = CLKComplicationTemplateCircularSmallRingText()
template.textProvider = CLKSimpleTextProvider(text: "12")
let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
handler(timelineEntry)
} else if complication.family == .modularLarge {
let template = CLKComplicationTemplateModularLargeStandardBody()
template.headerTextProvider = CLKSimpleTextProvider(text: "Main Header")
template.body2TextProvider = CLKSimpleTextProvider(text: "Main complication")
template.body1TextProvider = CLKSimpleTextProvider(text: "Sub complication 1")
let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
handler(timelineEntry)
} else {
handler(nil)
}
}
func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void)
{
switch complication.family
{
case .circularSmall:
let template = CLKComplicationTemplateCircularSmallRingText()
template.textProvider = CLKSimpleTextProvider(text: "12")
handler(template)
case .modularLarge:
let template = CLKComplicationTemplateModularLargeStandardBody()
template.headerTextProvider = CLKSimpleTextProvider(text: "Dummy Header")
template.body2TextProvider = CLKSimpleTextProvider(text: "Dummy Body")
template.body1TextProvider = CLKSimpleTextProvider(text: "Dummy Body 1")
handler(template)
default:
handler(nil)
}
}
}
我正在使用Xcode版本11.4(11E146)和Watch OS 6在Xcode中附加“并发症配置设置”的屏幕截图。
您的代码看起来不错(我没有尝试运行它,但是写了一些静态和动态并发症)。首先安装并发症模板,然后将其缓存。如果您第一次运行应用程序时没有遇到任何麻烦,或者遇到了复杂的问题,那么新的安装覆盖图将不会将其刷新为新版本。这是练习:
祝你好运