将 GraphicCircular Apple Watch 复杂功能添加到现有 Xcode 项目
Posted
技术标签:
【中文标题】将 GraphicCircular Apple Watch 复杂功能添加到现有 Xcode 项目【英文标题】:Adding GraphicCircular Apple Watch Complications to an existing Xcode Project 【发布时间】:2018-10-12 02:46:40 【问题描述】:我在网上找不到任何文档来解释如何在 Watch Series 4 上添加任何新的“图形”复杂功能。这些是我的步骤:
(1) 添加了符合CLKComplicationDataSource
的类(代码如下)
(2)设置并发症配置指向(1)和并发症资产文件夹
(4) 从 Sketch 导出 png 并拖入 Complications 资源文件夹到 Modular/Utilitarian/Circular
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
【问题讨论】:
【参考方案1】:我的问题是我没有实现getLocalizableSampleTemplate(for:withHandler:)
https://developer.apple.com/documentation/clockkit/clkcomplicationdatasource/1650686-getlocalizablesampletemplate,而它被列为可选而不是CLKComplicationDataSource
实现它的必需方法的一部分,这使我的图像出现在并发症中。
【讨论】:
这也帮助了我。如果没有方法调用,图像将不会显示。我尝试了 PDF 和个人尺寸。但这是解决方案【参考方案2】:下一个转换:
UIImage 名称:“GraphicBezel”->“Complication/GraphicBezel” UIImage 名称:“GraphicCircular”->“Complication/GraphicCircular” UIImage 名称:“GraphicCorner”->“Complication/GraphicCorner”图像资源、配置图像集 > 屏幕宽度 > 单个宽度 并将图像设置为观看尺寸区域(38mm、40mm、42mm、44mm)。
这对我的情况有用。
【讨论】:
以上是关于将 GraphicCircular Apple Watch 复杂功能添加到现有 Xcode 项目的主要内容,如果未能解决你的问题,请参考以下文章
Safari(或 Apple OS)将 .txt 添加到 .csv 下载