如何在watchOS的复杂功能中显示图片

9

我正在为我的watchOS 2应用程序设置复杂的功能。

我想提供三种不同类型的复杂功能:

  • 实用小型
  • 模块化最小项
  • 圆形小型

所有这些复杂类型都应仅显示我的应用程序图标作为图像。在我的ClockKit类中,我已经实现了以下方法:

func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimelineEntry?) -> Void) {

    if complication.family == .CircularSmall {

        let template = CLKComplicationTemplateCircularSmallRingImage()
        template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
        let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: template)
        handler(timelineEntry)

    } else if complication.family == .UtilitarianSmall{

        let template = CLKComplicationTemplateUtilitarianSmallRingImage()
        template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
        let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: template)
        handler(timelineEntry)

    } else if complication.family == .ModularSmall {

        let template = CLKComplicationTemplateModularSmallRingImage()
        template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
        let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: template)
        handler(timelineEntry)

    } else {

        handler(nil)

    }

}

我不确定这是否是实现我的想法的适当方式,因此我想知道仅显示图像作为我的复杂化的代码。有人知道我如何实现这个吗?

1个回答

14

首先,我强烈建议您观看一下苹果公司有关“复杂性”的视频演示,除非您还没有看过。

您至少需要在ComplicationController中实现以下3个非可选的CLKComplicationDataSource方法:

public func getSupportedTimeTravelDirectionsForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimeTravelDirections) -> Void)
public func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimelineEntry?) -> Void)
public func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void)

所有其他方法都是可选的。就您目前所看到的,您只实现了第二种方法。在您的上下文中,实现其余两种方法可能如下:

class ComplicationController: NSObject, CLKComplicationDataSource {

    func getSupportedTimeTravelDirectionsForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimeTravelDirections) -> Void) { 
        // Turn off time travelling
        handler([CLKComplicationTimeTravelDirections.None])
    }

    func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
        var template: CLKComplicationTemplate?

        switch complication.family {
            case .CircularSmall:
                template = CLKComplicationTemplateCircularSmallRingImage()
                template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
            case .UtilitarianSmall:
                template = CLKComplicationTemplateUtilitarianSmallRingImage()
                template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
            case .ModularSmall:
                template = CLKComplicationTemplateModularSmallRingImage()
                template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
            case .ModularLarge:
                template = nil
            case .UtilitarianLarge:
                template = nil
        }

        handler(template)
    }

}

别忘了在复杂配置中指定你的数据源类为$(PRODUCT_MODULE_NAME).ComplicationController并勾选适当的复选框。 在此输入图片描述

这是你情况下的最小复杂配置。


谢谢你详细的回答 - 也许我没有澄清,我已经实现了这些方法,否则我就无法构建这个应用程序。我会看一下你建议的WWDC会议视频。 - fredpi
@pi1000 - 已更新答案,并附上了配置截图。我认为您需要列举出您所做的所有事情,包括我附加的设置。 - Dmytro Hutsuliak
谢谢,但仍有一些误解。我将复杂性视为一个圆形,但我不想它具有这样的模板,并想知道是否可以使用我的应用程序图标作为图像。 - fredpi
4
我终于明白你想要什么了 :) 不,你不能直接在复杂功能中使用你的应用程序标志-你需要适应一个只使用一种颜色和透明度 (alpha 通道) 的标志。请仔细听从会议视频,从12:10开始,否则你将只得到一个被颜色淹没的图标(是的-那个圆形就是你的应用程序标志)。 - Dmytro Hutsuliak
好的,感谢您的帮助和耐心 - 我一定会尝试一下。 - fredpi
1
不需要使用 $(PRODUCT_MODULE_NAME).ComplicationController,只需在配置中使用 ComplicationController。Complications Group 是各种尺寸图像所在的文件夹名称,因此如果是“Complication”,请使用此名称;如果没有文件夹,请使用上述“none”。图像的名称必须被引用为“FolderName/ImageName”,例如“Complication/Modular”等,但要注意,在导入 XCode 时,它使用原始的“Modular”等名称而不是文件名!您可以将其重命名,但是考虑到混乱的 Complication 问题和繁忙的 API 更改,我的建议是将其保留为原样! - BootMaker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接