菜鸟教程小白 发表于 2022-12-11 17:06:00

swift - WatchKit 2 并发症文本仅在预览中显示


                                            <p><p>我正在尝试为 <a href="https://stackoverflow.com/questions/tagged/watchkit" class="post-tag" title="show questions tagged &#39;watchkit&#39;" rel="noreferrer noopener nofollow">watchkit</a> 开发一个非常简单的并发症。 2 从一个简单的文本提供程序说“嗨”。</p>

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

<p> <a href="/image/TaRVD.gif" rel="noreferrer noopener nofollow"><img src="/image/TaRVD.gif" alt="watchface behaviing strangely gif"/></a> </p>

<p>任何想法可能导致这种情况?</p>

<p>我的文本提供程序如下所示</p>

<pre><code>var textProvider: CLKSimpleTextProvider

override init() {

    textProvider = CLKSimpleTextProvider()
    textProvider.text = &#34;Hi&#34;
    textProvider.shortText = &#34;HI&#34;
    textProvider.tintColor = UIColor.whiteColor()
    super.init()
}
</code></pre>

<p>而我的 <code>getPlaceholderTemplateForComplication</code> 看起来像</p>

<pre><code>func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -&gt; 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)
    }

}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在自定义表盘时,Apple Watches 调用 <code>getPlaceholderTemplateForComplication:withHandler:</code> 来显示占位符文本。既然你已经实现了它 - 你可以看到“嗨”。太棒了。
但是当表盘显示时,它会调用另一个方法,例如:</p>

<ul>
<li><code>getCurrentTimelineEntryForComplication:withHandler:</code></li>
<li><code>getTimelineEntriesForComplication:beforeDate:limit:withHandler:</code></li>
<li><code>getTimelineEntriesForComplication:afterDate:limit:withHandler:</code></li>
</ul>

<p>而且您似乎没有实现它们。因此,实现这些方法将解决您的问题。
您可以在此 WWDC 2015 教程中找到有关这些方法的更多详细信息:<a href="https://developer.apple.com/videos/wwdc/2015/?id=209" rel="noreferrer noopener nofollow">https://developer.apple.com/videos/wwdc/2015/?id=209</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于swift - WatchKit 2 并发症文本仅在预览中显示,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38571743/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38571743/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: swift - WatchKit 2 并发症文本仅在预览中显示