2017-05-13 72 views
0

我希望有人能幫助我解決這個問題。以下是我的tvOS應用截圖:嘗試在collectionview單元格之間切換焦點時更新UI元素

Screenshot

我試圖做兩件事情:

  1. 當用戶向下滾動他們的遙控器,在的CollectionView第二項變爲自動聚焦。我需要這樣做,以便第一個項目的重點。

  2. 當用戶在collectionView中的項目之間手動移動焦點時,我希望屏幕中間部分的UI元素得到更新,而不必單獨按下或選擇它們。

這是迄今爲止對這個畫面我的代碼:

import UIKit 

// Global variable 
internal let g_CR1 = "channel_cell_01" 

class DashboardVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 

    // outlets 
    @IBOutlet weak var collectionView: UICollectionView! 
    @IBOutlet weak var lblChannelName: UILabel! 
    @IBOutlet weak var imgChannelLogo: UIImageView! 
    @IBOutlet weak var txtChannelDescription: UITextView! 

    // variables 
    var staticData: [Channel] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // sample channels 
     initializeSampleData() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    // MARK: - CollectionView Callbacks 
    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return staticData.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     // Get or make a cell 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: g_CR1, for: indexPath) as! CollectionViewCell 

     // Configure 
     cell.imgView.backgroundColor = UIColor.black 
     cell.imgView.image = staticData[indexPath.row].chLogo 

     // Return. 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { 
     return true 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

     let currentChannel = staticData[indexPath.row] 

     DispatchQueue.main.async { 
      self.lblChannelName.text = currentChannel.chName 
      self.imgChannelLogo.image = currentChannel.chLogo 
      self.txtChannelDescription.text = currentChannel.chDescription 
     } 
    } 

} 

回答

0

我能回答點#2。希望有人可以從中受益。不過,我仍然排在第一。

func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) { 

    // test 
    print("Previous Focused Path: \(context.previouslyFocusedIndexPath)") 
    print("Next Focused Path: \(context.nextFocusedIndexPath)") 

    let currentChannel = staticData[(context.nextFocusedIndexPath?[1])!] 

    DispatchQueue.main.async { 
     self.lblChannelName.text = currentChannel.chName 
     self.imgChannelLogo.image = currentChannel.chLogo 
     self.txtChannelDescription.text = currentChannel.chDescription 
    } 
} 
相關問題