2015-09-25 22 views
6

我目前正在爲我的watchOS 2應用程序設置複雜功能。如何在watchOS併發症中顯示圖像

我想提供三種不同類型的併發症:

  • 功利小
  • 模塊化smallist項目
  • 圓形小

所有這些複雜類型應該只顯示我的應用程序圖標,一個圖像。在我ClockKit類,我已實現了以下方法:

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) 

    } 

} 

我不知道這是實現我的想法的適當的方式,所以我想知道的代碼只是顯示圖像作爲我的併發症。有人知道我能做到嗎?

回答

10

起初,我強烈建議您觀看有關併發症的Apple's session,除非您還沒有看到它。

您需要實現以下3個的CLKComplicationDataSource非可選方法,您ComplicationController至少:

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) 
    } 

} 

而且不要忘了併發症的配置來指定數據源類爲$(PRODUCT_MODULE_NAME).ComplicationController,並檢查相應的複選框。 enter image description here

這是您的案例中最小的複雜配置。

+0

感謝您的詳細解答 - 也許我沒有說明我已經實施了這些方法,否則我無法構建應用程序。我將看看您建議的WWDC會話視頻。 – fredpi

+0

@ pi1000 - 用配置屏幕截圖更新了答案。我認爲你需要列舉你所做的所有事情,包括我附加的設置。 –

+0

是的,謝謝,但還是有些誤解。我將複雜度視爲一個圓形的圓圈,但我不希望它具有這樣的模板,並且想知道我是否可以將我的應用程序圖標用作圖像。 – fredpi