如何在 watchOS 复杂功能中显示图像
How to display image in watchOS complication
我目前正在为我的 watchOS 2 应用程序设置复杂功能。
我想提供三种不同类型的并发症:
- 功利小
- 模块化小型项目
- 圆形小
所有这些复杂类型都应该简单地将我的应用程序图标显示为图像。在我的 ClockKit class 中,我实现了以下方法:
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)
}
}
并且不要忘记在复杂配置中将您的数据源 Class 指定为 $(PRODUCT_MODULE_NAME).ComplicationController
并选中相应的复选框。
这是您的情况下的最小并发症配置。
我目前正在为我的 watchOS 2 应用程序设置复杂功能。
我想提供三种不同类型的并发症:
- 功利小
- 模块化小型项目
- 圆形小
所有这些复杂类型都应该简单地将我的应用程序图标显示为图像。在我的 ClockKit class 中,我实现了以下方法:
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)
}
}
并且不要忘记在复杂配置中将您的数据源 Class 指定为 $(PRODUCT_MODULE_NAME).ComplicationController
并选中相应的复选框。
这是您的情况下的最小并发症配置。