swift – 如何在watchOS复杂中显示图像

我目前正在为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)

    }

}

我不确定这是实现我的想法的合适方式,所以我想知道将图像显示为我的复杂功能的代码.有谁知道我怎么能做到这一点?

起初我强烈建议你观看 Apple’s session关于并发症,除非你还没有看到它.

您至少需要在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)
    }

}

不要忘记在Complication Configuration中将您的数据源类指定为$(PRODUCT_MODULE_NAME).ComplicationController并选中相应的复选框.

这是您案例中最小的复杂配置.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...