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

问题描述 投票:0回答:1

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

Screenshot(来源:mtiaz.com

我正在尝试做两件事:

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

}
swift3 tvos
1个回答
0
投票

我能够回答第二点。希望有人可以从中受益。但是我仍然被#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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.