2016-12-14 55 views
1

任何熟悉Carekit的人都可以引導我如何在護理卡的說明部分顯示視頻。有一個選項可以使用OCKCarePlanActivity類而不是視頻通過imageURL添加照片。 謝謝!在CareKit中顯示視頻。

+0

您必須使用自己的自定義詳細視圖控制器來顯示視頻。 – tktsubota

+0

有人在乎細說嗎? –

回答

0

下面的代碼顯示了一個打開(故事板創建的)UIViewController如何創建一個簡單的活動,將它添加到Care Plan Store並顯示標準的OCKCareCardViewController。 (這個結構大部分來自「Begin CareKit開發」)。視圖控制器只有一個按鈕,其功能是顯示標準保健卡:

OpeningViewController - just one button

所述保健卡是完全標準,如下所示。但是,如果您單擊事件按鈕,它將顯示一個自定義視圖控制器。

Standard OCKCareCardViewController

定製視圖控制器,如下所示,內置在情節串連圖板。自定義干預是微不足道的 - 它只是要求用戶點擊構成場景的樹形按鈕。 (你可以有一個視頻播放器視圖,而不是三個按鈕)。

Custom view controller TapThreeButtonsViewController

幾乎所有的代碼是在OpeningViewController類。這個類如下所示。

// 
// OpeningViewController.swift 
// 


/* 
Context: 
    This app demonstrates how to display a custom UIViewController when 
    the user taps on an event in a CareCard. 

    This app manages just one, hard-coded intervention activity. When 
    the user taps on an event-button (usually an open circle) on the 
    main Care Card then the app displays a custom UIViewController 
    with three buttons for the user to tap. 

Purpose: 
    This root view controller for the app shows a button that allows 
    the user to request a standard OCKCareCardViewController. 

    Additionally: 
     1. In the init for this view controller an instance of 
      OCKCarePlanStore is created. 
      (Note: this is poor design, ordinarily you would probably 
      have a singleton model for the Care Plan Store. But as long 
      as you don't re-initialize this view it will serve for demo 
      purposes). 
     2. During viewDidLoad a convenience function is called that 
      will: 
      A. Check to see if an activity with an identifier of 
       "TapThreeButtons" exists 
      B. if not, it builds an activity scheduled to begin three 
       days ago. 
      C. The new intervention activity is added to the store. 
     3. In the ShowCareCard IBAction: 
      A. an instance of OCKCareCardViewController is built 
      B. the delegate for the new Care Card is set to self. 
      C. the new Care Card is presented modally 
     4. An extension is used to make this ViewController conform 
      to OCKCareCardViewControllerDelegate. 
      A. implements the optional method 
       careCardViewController(VC: 
           didSelectButtonWithInterventionEvent:) 
       THIS IS WHERE A CUSTOM DISPLAY CAN BE DISPLAYED TO 
       RESPOND TO THE TAPPING OF AN EVENT. 
      B. Returns "false" from 
       careCardViewController: 
           shouldHandleEventCompletionForActivity: 
       This prevents CareKit from automatically processing an 
       event when an event button is tapped. 
*/ 

import UIKit 
import CareKit 

struct Global { 
    static let tapButtonsIdentifier = "TapThreeButtons" 
} 

class OpeningViewController: UIViewController { 

    var store: OCKCarePlanStore 

    required init(coder aDecoder: NSCoder) 
    { 

     // 1. get a URL for the Document directory, then a URL for 
       the CarePlanStore 
     let fileManager = FileManager.default 
     guard let documentDirectory 
      = fileManager.urls(for: .documentDirectory, 
           in: .userDomainMask).last 
      else { 
       let msg = "*** Error: FileManager is not returning the document directory URL *** " 
       fatalError(msg) 
     } 
     let storeURL = 
      documentDirectory 
       .appendingPathComponent("TapInterventionStore") 

     // 2. create the store directory if it does not already exist 
     if !fileManager.fileExists(atPath: storeURL.path) { 
      try! fileManager.createDirectory(at: storeURL, 
            withIntermediateDirectories: true, 
            attributes: nil) 
     } 
     // 3. set an instance constant to point to the store 
     store = OCKCarePlanStore(persistenceDirectoryURL: storeURL) 
     super.init(coder: aDecoder)! 
    } 

    @IBAction func ShowCareCard(_ sender: UIButton) { 
     let careCardViewController = createCareCardViewController() 
     careCardViewController.delegate = self 
     let embeddedCareCard = 
      UINavigationController(rootViewController: 
       careCardViewController) 
     // present the CareCard modally 
     self.present(embeddedCareCard, 
         animated: true, 
         completion: nil) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // if no "tapThreeTimes" activity exists in CarePlanStore then 
     // create one 
     createActivity() 
    } 

    // MARK: Convenience Methods 

    fileprivate func createActivity() 
    { 
     // query the store to see if there already exists an activity 
     // with this identifier 
     store.activity(forIdentifier: Global.tapButtonsIdentifier) 
     { 
      [ unowned self ] 
      (success, foundActivity, error) in 
      // check that the query terminated successfully 
      guard success else { 
       let msg = "A query of CarePlanStore for the \(Global.tapButtonsIdentifier) intervention activity produced an error." 
       fatalError(msg) 
      } 
      // check to see if the activity is in CarePlanStore 
      if foundActivity == nil { 
       // instatiate a new activity 
       let newTapButtonsActivity 
        = self.buildNewTapButtonsActivity() 
       // 4. save the new Activity instance, using the completion to check that the save worked 
       self.store.add(newTapButtonsActivity) 
       { 
        (success, error) in 
        guard success else { 
         let msg = "An error was returned while saving a new \(Global.tapButtonsIdentifier) intervention activity." 
         fatalError(msg) 
        } 
       }   // ends new activity closure 
      }   // ends check that activity might be nil 
     }   // ends closure on query for activity in store 
    } 

    fileprivate func buildNewTapButtonsActivity() 
             -> OCKCarePlanActivity 
    { 

     struct Lets { 
      static let title = "Push Three Buttons" 
      static let text = "Tap one of the circles below and you'll be asked to tap each of three buttons" 
      static let instructions = "When you tap on an event (those circles on your Care Card) the system will show you a screen with three buttons. Tap all three. They can be tapped in any sequence but try not to tap the same button twice." 
     } 

     // create events by starting three days ago, 
     // with 2 events per day 
     let gregorianCalendar = Calendar(identifier: .gregorian) 
     guard let threeDaysAgo = 
      gregorianCalendar.date(byAdding: .day, 
            value: -3, 
            to: Date()) 
     else { 
      let msg = "Could not calculate date for three-days-before-today." 
      fatalError(msg) 
     } 
     let startDate = 
      gregorianCalendar.dateComponents([.year, .month, .day ], 
              from: threeDaysAgo) 
     let thriceADay = 
      OCKCareSchedule.dailySchedule(withStartDate: startDate, 
              occurrencesPerDay: 2) 
     let activity = 
      OCKCarePlanActivity(
       identifier:  Global.tapButtonsIdentifier, 
       groupIdentifier: nil, 
       type:   .intervention, 
       title:   Lets.title, 
       text:   Lets.text, 
       tintColor:  nil, 
       instructions: Lets.instructions, 
       imageURL:  nil, 
       schedule:  thriceADay, 
       resultResettable: true, 
       userInfo:  nil) 
     return activity 
    } 

    fileprivate func createCareCardViewController() 
          -> OCKCareCardViewController 
    { 
     let viewController = 
      OCKCareCardViewController(carePlanStore: self.store) 
     // Setup the controllers title 
     viewController.title = 
      NSLocalizedString("Tap Test App", 
      comment: "This app shows CareKit users how to add a custom interface to Care Card") 

     return viewController 

    } 
} 

extension OpeningViewController: OCKCareCardViewControllerDelegate { 
    /** 
    prevents CareKit from automatically coloring-in an event symbol when tapped 
    - parameter viewController: the view controller that displays the events 
    - parameter shouldHandleEventCompletionForActivity: the activity associated with the tapped event 
    */ 
    func careCardViewController( 
      _ viewController: OCKCareCardViewController, 
      shouldHandleEventCompletionFor interventionActivity: 
       OCKCarePlanActivity) 
     -> Bool 
    { 
     return false 
    } 

    /** 
    modally presents a custom UIViewController when the user taps an event button on the Care Card 
    - parameter viewController: the OCKCareCardViewController master/detail that presents the event 
    - parameter didSelectRowWithInterventionActivity: the user selected OCKCarePlanActivity tapped by the user 
    - Note in this implementation the new detail view controller will be an instance of ZCCareCardDetailViewController showing the "medication" and an associated medication image 
    */ 
    func careCardViewController(_ viewController: OCKCareCardViewController, 
           didSelectButtonWithInterventionEvent interventionEvent: OCKCarePlanEvent) 
    { 
     // In this simplistic app, assume that a user-tap on an already completed event always 
     // means that the user just wants to "clear" the event 
     if interventionEvent.state == .completed { 
      store.update(interventionEvent, 
          with: interventionEvent.result, 
          state: .notCompleted) 
      { 
       (success, event, error) in 
       if success { 
        print("toggled the event's state") 
       } else { 
        print("failed to toggle event's state") 

       } 
      } 
     } else { 

      // 1. Create the custom detail view controller 
      let storyBoard = UIStoryboard(name: "Main", bundle: nil) 
      let tapThreeButtonsViewController = 
       storyBoard.instantiateViewController(
        withIdentifier: Global.tapButtonsIdentifier) as! 
         TapThreeButtonsViewController 

      // 2 set up the data model for the ViewController 
      tapThreeButtonsViewController.carePlanEvent 
       = interventionEvent 
      tapThreeButtonsViewController.store = store 

      // 3. Embed in a UINavigationController 
      let embeddedViewController = 
       UINavigationController( 
        rootViewController: tapThreeButtonsViewController) 
      // 4. modally present the custom detail view controller 
      viewController.present(embeddedViewController, 
            animated: true, 
            completion: nil) 
     } 
    } 
} 

自定義視圖控制器的代碼並不特別有趣(或者,就此而言,非常多的調試)!無論如何,我會展示它,因爲我認爲這是您保留自定義干預產生的詳細數據的方式。

// 
    // TapThreeButtonsViewController.swift 
    // CustomCareCard 
    // 
    /* 
    Context: 
     This app is meant to show how a custom view controller can be 
     displayed in the middle of a CareKit app. 

    Purpose: 
     This class defines the custom view controller built in the 
     storyboard. It has these elements: 
      1. A Done navigation button to indicate that the intervention 
       activity has been completed. 
      2. A Cancel navigation button to indicate that the user is not 
       interested in completing the activity. 
      3. A label giving the user instructions to tap on three 
       buttons (kind of silly, really, but the point here is to 
       show how to display a custom interface not to solve a real 
       medical problem). 
      4. Three buttons labeled "Button One", "Button Two", 
       "Button Three" for the user to tap. 

     The only significant content in this view controller comes in the 
"doneButton" action. The code there shows you how to save a basic "value" 
to record the user's actions. Here the "value" is equal to the total number 
of times the threee buttons were tapped. Additionally, the code will save 
the number of times each individual button was tapped, using the "userInfo" 
property of the OCKCarePlanEventResult object. Detail data of this sort 
could be useful for formulating OCKInsightViewController reports. 
    */ 


    import UIKit 
    import CareKit 

    class TapThreeButtonsViewController: UIViewController { 

     // MARK: data model 

     public var carePlanEvent: OCKCarePlanEvent! 
     public var store: OCKCarePlanStore! 


     fileprivate var tapCount = [ "Button One": 0, 
            "Button Two": 0, 
            "Button Three": 0 
            ] 

     // MARK: actions 


     /** 
     when the user taps any of the three main buttons this logic adds to the count of that button 
     - parameter sender: the UIButton that was tapped (Button One, Button Two or Button Three) 
     */ 
     @IBAction func tapButton(_ sender: UIButton) { 
      let buttonName = sender.currentTitle! 
      tapCount[ buttonName ]! += 1 
     } 

     /** 
     saves the total number of button taps as the event "value", but also saves the counts-for-each-button in "userInfo" 
     - parameter sender: the UIButtonItem that initiated the action 
     */ 
     @IBAction func doneButton(_ sender: UIBarButtonItem!) { 
      var totalTaps = 0 
      var codableTapCount: Dictionary< String, NSNumber > = [ : ] 
      // CAREKIT reminder: "values" are stored as strings. The 
      //   userInfo dictionary requires that the 
      //   the "value" follow NSCoder requirements. 
      for (button, taps) in tapCount { 
       totalTaps += taps 
       codableTapCount[ button ] = NSNumber(value: taps) 
      } 
      let results = 
       OCKCarePlanEventResult(
        valueString: totalTaps.description, 
        unitString: "taps", 
        userInfo: codableTapCount) 
      store.update(carePlanEvent, 
          with: results, 
          state: .completed) { 
       // use completion event simply to check that the Care 
       // Plan Store updated successfully 
       (success, event, error) in 
       if !success { 
        fatalError("The Care Plan Store was not updated with the most-recent results from the Tap Buttons test.") 
       } 
      } 
      self.dismiss(animated: true) 
     } 

     /** 
     Dismisses the custom view controller without making changes to the Care Plan Store 
     */ 
     @IBAction func cancelButton(_ sender: AnyObject) { 
      self.dismiss(animated: true) 
     }  
    }