swift - WatchKit 2 复杂文本仅在预览中显示

标签 swift watchkit apple-watch apple-watch-complication

我正在尝试为 开发一个非常简单的复杂功能2 来自简单文本提供程序的“嗨”。

我设法实现了一些奇怪的行为;单击复杂功能或从自定义表盘屏幕预览它时,我可以看到文本,但在显示表盘时看不到。看看:

watchface behaviing strangely gif

知道是什么原因造成的吗?

我的文本提供者看起来像这样

var textProvider: CLKSimpleTextProvider

override init() {

    textProvider = CLKSimpleTextProvider()
    textProvider.text = "Hi"
    textProvider.shortText = "HI"
    textProvider.tintColor = UIColor.whiteColor()
    super.init()
}

我的 getPlaceholderTemplateForComplication 看起来像

func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
    // This method will be called once per supported complication, and the results will be cached

     switch complication.family {
     case .ModularSmall:
        let stemplate = CLKComplicationTemplateModularSmallSimpleText()
        stemplate.tintColor = UIColor.whiteColor()
        stemplate.textProvider = textProvider
        handler(stemplate)
     case .CircularSmall:
        let stemplate = CLKComplicationTemplateCircularSmallSimpleText()
        stemplate.tintColor = UIColor.whiteColor()
        stemplate.textProvider = textProvider
        handler(stemplate)
     default:
        handler(nil)
    }

}

最佳答案

在自定义表盘时,Apple Watches 会调用 getPlaceholderTemplateForComplication:withHandler: 来显示占位符文本。因为你已经实现了它 - 你可以看到“嗨”。太棒了。 但是当表盘显示时,它会调用另一个方法,例如:

  • getCurrentTimelineEntryForComplication:withHandler:
  • getTimelineEntriesForComplication:beforeDate:limit:withHandler:
  • getTimelineEntriesForComplication:afterDate:limit:withHandler:

而且您似乎还没有实现它们。因此,实现这些方法将解决您的问题。 您可以在此 WWDC 2015 教程中找到有关这些方法的更多详细信息:https://developer.apple.com/videos/wwdc/2015/?id=209

关于swift - WatchKit 2 复杂文本仅在预览中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32443304/

相关文章:

watchkit - 在哪里可以找到 watchOS 2 的 Taptic 反馈 API 文档或功能?

xcode - 如何在 Apple Watch OS 2 上触发振动?

ios - 将音频从 Watch 流式传输到 iPhone 以使用 SFSpeechRecognizer

ios - Swift - 创建一个没有导航 Controller 的后退按钮

ios - Swift:UITableViewController 中的 PrototypeCells 问题

ios - 'openParentApplication(_ :reply:)' has been explicitly marked unavailable here - Xcode 7 Beta

ios - 如何在 iOS 中设置 Apple Watch 图标图像

json - 如果用户更新个人资料图像,则更新 JSON 中的键值

ios - 如何设置UITableViewCell的imageView?

iphone - Apple Watch 中的布局