Gar*_*abo 5 xcode swift apple-watch watchos apple-watch-complication
我在网上找不到任何文档来解释如何添加 Watch Series 4 上提供的任何新的“图形”复杂功能。以下是我的步骤:
(1) 添加符合的类CLKComplicationDataSource(下面的代码) (2) 将并发症配置设置为指向 (1) 和 Complications asset 文件夹
(4) 从 Sketch 导出 png 并拖动到 Complications 资源文件夹中,以使用 Complications 资源文件夹中的模块化/实用/圆形图形(角、边框和圆形)占位符,该占位符不接受 png(仅限 pdf)。
毕竟,旧的模块化实用和圆形复杂功能工作正常,但是图形(角、边框和圆形)的图像(pdf)不会在设备上呈现
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,我没有实现getLocalizableSampleTemplate(for:withHandler:) https://developer.apple.com/documentation/clockkit/clkcomplicationdatasource/1650686-getlocalizedsampletemplate,而它被列为可选,而不是实现它所需的方法的一部分,CLKComplicationDataSource这使得我的图像显示在并发症。
| 归档时间: |
|
| 查看次数: |
2759 次 |
| 最近记录: |