将 GraphicCircular Apple Watch 复杂功能添加到现有 Xcode 项目
Adding GraphicCircular Apple Watch Complications to an existing Xcode Project
我在网上找不到任何说明如何在 Watch Series 4 上添加任何新的 "graphic" 复杂功能的文档。这些是我的步骤:
(1) 添加了符合 CLKComplicationDataSource
的 class (代码如下)
(2) 将并发症配置设置为指向 (1) 和并发症资产文件夹
(4) 从 Sketch 导出 png 并拖入 Complications asset 文件夹到 Modular/Utilitarian/Circular
Complications 资产文件夹中的图形(角、边框和圆形)占位符不接受 png(仅 pdf)。 After all this the old Modular Utilitarian and Circular complications work fine, however the images (pdfs) for the Graphics (Corner, Bezel, and Circular) do not render on device
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
实施它的必需方法的一部分使我的图像显示在并发症。
下一步转换:
- UIImage 名称:"GraphicBezel" -> "Complication/GraphicBezel"
- UIImage 名称:"GraphicCircular" -> "Complication/GraphicCircular"
- UIImage 名称:"GraphicCorner" -> "Complication/GraphicCorner"
图像资源,配置图像集 > 屏幕宽度 > 单个宽度
并将图像设置为观看尺寸区域(38mm、40mm、42mm、44mm)。
它适合我的情况。
我在网上找不到任何说明如何在 Watch Series 4 上添加任何新的 "graphic" 复杂功能的文档。这些是我的步骤:
(1) 添加了符合 CLKComplicationDataSource
的 class (代码如下)
(2) 将并发症配置设置为指向 (1) 和并发症资产文件夹
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
实施它的必需方法的一部分使我的图像显示在并发症。
下一步转换:
- UIImage 名称:"GraphicBezel" -> "Complication/GraphicBezel"
- UIImage 名称:"GraphicCircular" -> "Complication/GraphicCircular"
- UIImage 名称:"GraphicCorner" -> "Complication/GraphicCorner"
图像资源,配置图像集 > 屏幕宽度 > 单个宽度 并将图像设置为观看尺寸区域(38mm、40mm、42mm、44mm)。
它适合我的情况。