我在网上找不到任何文档来解释如何在 Watch Series 4 上添加任何新的“图形”复杂功能。这些是我的步骤:

(1) 添加了符合 CLKComplicationDataSource 的类(代码如下)
(2)设置并发症配置指向(1)和并发症 Assets 文件夹
swift - 将 GraphicCircular Apple Watch 复杂功能添加到现有的 Xcode 项目-LMLPHP
(4)从Sketch导出png并拖入Complications Assets 文件夹到Modular/Utilitarian/Circular
Complications Assets 文件夹中的图形(Corner、Bezel 和 Circular)占位符不接受 png(仅 pdf)。 swift - 将 GraphicCircular Apple Watch 复杂功能添加到现有的 Xcode 项目-LMLPHP 毕竟,旧的模块化实用程序和圆形复杂功能可以正常工作,但是图形(角、边框和圆形)的图像 (pdf) 不会在设备上呈现 swift - 将 GraphicCircular Apple Watch 复杂功能添加到现有的 Xcode 项目-LMLPHP

import Foundation
import ClockKit

class HockeyTrackerComplication: NSObject, CLKComplicationDataSource {
    func getSupportedTimeTravelDirections(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimeTravelDirections) -> Void) {
        handler([])
    }

    func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
        if #available(watchOSApplicationExtension 5.0, *) {
            if complication.family == .circularSmall {

                let template = CLKComplicationTemplateCircularSmallRingImage()
                guard let image = UIImage(named: "Circular") else { handler(nil); return}
                template.imageProvider = CLKImageProvider(onePieceImage: image)
                let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
                handler(timelineEntry)

            } else if complication.family == .utilitarianSmall {

                let template = CLKComplicationTemplateUtilitarianSmallRingImage()
                guard let image = UIImage(named: "Utilitarian") else { handler(nil); return}
                template.imageProvider = CLKImageProvider(onePieceImage: image)
                let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
                handler(timelineEntry)

            } else if complication.family == .modularSmall {

                let template = CLKComplicationTemplateModularSmallRingImage()
                guard let image = UIImage(named: "Modular") else { handler(nil); return}
                template.imageProvider = CLKImageProvider(onePieceImage: image)
                let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
                handler(timelineEntry)

            } else if complication.family == .graphicCircular {
                let template = CLKComplicationTemplateGraphicCircularImage()
                guard let image = UIImage(named: "GraphicCircular") else { handler(nil); return}
                template.imageProvider = CLKFullColorImageProvider(fullColorImage: image)
                let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
                handler(timelineEntry)

            } else if complication.family == .graphicBezel {
                let template = CLKComplicationTemplateModularSmallRingImage()
                guard let image = UIImage(named: "GraphicBezel") else { handler(nil); return}
                template.imageProvider = CLKImageProvider(onePieceImage: image)
                let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
                handler(timelineEntry)

            } else if complication.family == .graphicCorner {
                let template = CLKComplicationTemplateGraphicCornerCircularImage()
                guard let image = UIImage(named: "GraphicCorner") else { handler(nil); return}
                template.imageProvider = CLKFullColorImageProvider(fullColorImage: image)
                let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
                handler(timelineEntry)

            } else {

                handler(nil)

            }
        } else {
            // Fallback on earlier versions
        }
    }



}

最佳答案

我的问题是我没有实现 getLocalizableSampleTemplate(for:withHandler:) https://developer.apple.com/documentation/clockkit/clkcomplicationdatasource/1650686-getlocalizablesampletemplate 而它被列为可选,而不是 CLKComplicationDataSource 实现它的必需方法的一部分,这使得我的图像出现在并发症中。

关于swift - 将 GraphicCircular Apple Watch 复杂功能添加到现有的 Xcode 项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52771463/

10-09 01:36