在 collectionview 单元格之间切换焦点时尝试更新 UI 个元素

Trying to update UI elements when switching focus between collectionview cells

我希望有人能帮我解决这个问题。下面是我的 tvOS 应用截图:


(来源:mtiaz.com
.

我正在尝试做两件事:

  1. 当用户在遥控器上向下滚动时,collectionView 中的第二个项目会自动获得焦点。我需要它以便第一个项目成为焦点。

  2. 当用户在 collectionView 中的项目之间手动移动焦点时,我希望屏幕中间部分的 UI 元素得到更新,而不必按或 select他们一个个。

到目前为止,这是我为此屏幕编写的代码:

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

}

我能够回答第 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
    }
}