Swift   发布时间:2022-04-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – 启动App的WatchOS3复杂功能大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想为watchOS 3创建一个复杂功能,只需启动我的应用程序.我用XCode创建了ComplicationController:

class ComplicationController: NSObject,CLKComplicationDatasource
{

    // MARK: - Timeline Configuration

    func getSupportedTimeTravelDirections(for complication: CLKComplication,withHandler handler: @escaping (CLKComplicationTimeTravelDirections) -> Void) {
        handler([.forWARD,.BACkWARD])
    }

    func getTimelinestartDate(for complication: CLKComplication,withHandler handler: @escaping (Date?) -> Void) {
        handler(nil)
    }

    func getTimelineEndDate(for complication: CLKComplication,withHandler handler: @escaping (Date?) -> Void) {
        handler(nil)
    }

    func getPrivacyBehavior(for complication: CLKComplication,withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
        handler(.showOnLockScreen)
    }

    // MARK: - Timeline Population

    func getCurrentTimelineEntry(for complication: CLKComplication,withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
        // Call the handler with the current timeline entry
        handler(nil)
    }

    func getTimelineEntries(for complication: CLKComplication,before date: Date,limit: Int,withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
        // Call the handler with the timeline entries prior to the given date
        handler(nil)
    }

    func getTimelineEntries(for complication: CLKComplication,after date: Date,withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
        // Call the handler with the timeline entries after to the given date
        handler(nil)
    }

    // MARK: - Placeholder Templates

    func getLocalizableSampleTemplate(for complication: CLKComplication,withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
        // This method will be called once per supported complication,and the results will be cached
        handler(nil)
    }

}

并为Circular,Modular和Utilitarian资产添加了图像.但是当我运行Watch App时,我无法选择Watch脸的并发症.我还需要做什么?

谢谢

格雷格

解决方法

这些代码更改是必需的:

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


func getCurrentTimelineEntry(for complication: CLKComplication,withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void)
{
    if complication.family == .circularsmall
    {

        let template = CLKComplicationTemplateCircularsmallRingImage()
        template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "Circular")!)
        let timelineEntry = CLKComplicationTimelineEntry(date: Date(),complicationTemplate: templatE)
        handler(timelineEntry)

    } else if complication.family == .utilitariansmall
    {

        let template = CLKComplicationTemplateUtilitariansmallRingImage()
        template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "Utilitarian")!)
        let timelineEntry = CLKComplicationTimelineEntry(date: Date(),complicationTemplate: templatE)
        handler(timelineEntry)

    } else if complication.family == .modularsmall
    {

        let template = CLKComplicationTemplateModularsmallRingImage()
        template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "Modular")!)
        let timelineEntry = CLKComplicationTimelineEntry(date: Date(),complicationTemplate: templatE)
        handler(timelineEntry)

    } else {

        handler(nil)

    }

}


func getLocalizableSampleTemplate(for complication: CLKComplication,withHandler handler: @escaping (CLKComplicationTemplate?) -> Void)
{        
    switch complication.family
    {
        case .circularsmall:
            let image: UIImage = UIImage(named: "Circular")!
            let template = CLKComplicationTemplateCircularsmallSimpleImage()
            template.imageProvider = CLKImageProvider(onePieceImage: imagE)
            handler(templatE)
        case .utilitariansmall:
            let image: UIImage = UIImage(named: "Utilitarian")!
            let template = CLKComplicationTemplateUtilitariansmallSquare()
            template.imageProvider = CLKImageProvider(onePieceImage: imagE)
            handler(templatE)
        case .modularsmall:
            let image: UIImage = UIImage(named: "Modular")!
            let template = CLKComplicationTemplateModularsmallSimpleImage()
            template.imageProvider = CLKImageProvider(onePieceImage: imagE)
            handler(templatE)
        default:
            handler(nil)
    }
}

此外,您需要在扩展中提供图像作为资产.

大佬总结

以上是大佬教程为你收集整理的swift – 启动App的WatchOS3复杂功能全部内容,希望文章能够帮你解决swift – 启动App的WatchOS3复杂功能所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。