2017-05-13 1 views
0

誰かがこの問題を助けてくれることを願っています。以下は私のtvOSアプリのスクリーンショットです:コレクションビューのセル間でフォーカスを切り替えるときにUI要素を更新しようとしています

Screenshotです。私は二つのことを行うにしようとしています

  1. ユーザーがスクロールダウンし、そのリモコンのを、collectionViewの2番目の項目は自動的に焦点を当てになります。私は最初の項目が代わりに焦点を合わせるようにする必要があります。

  2. コレクションビュー内のアイテム間で手動でフォーカスを移動すると、画面の中央セクションの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に答えることができました。うまくいけば誰かがこれから恩恵を受けるかもしれないしかし、私はまだ#1で踏ん張っています。

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 
    } 
} 
関連する問題