Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 将UICollectionView焦点行置于tvOS上_Swift_Uicollectionview_Uicollectionviewcell_Tvos - Fatal编程技术网

Swift 将UICollectionView焦点行置于tvOS上

Swift 将UICollectionView焦点行置于tvOS上,swift,uicollectionview,uicollectionviewcell,tvos,Swift,Uicollectionview,Uicollectionviewcell,Tvos,我的tvOS应用程序中有一个UICollectionView,其中填充了几个UICollectionViewCells,这些单元格又包含一个UICollectionView,里面有自己的项目 容器UICollectionView垂直滚动,而内部UICollectionView水平滚动。顶级UICollectionViewCells禁用焦点,并将焦点传递到内部UICollectionView中的项目中 Before centering vertically -------------------

我的tvOS应用程序中有一个
UICollectionView
,其中填充了几个
UICollectionViewCell
s,这些单元格又包含一个
UICollectionView
,里面有自己的项目

容器
UICollectionView
垂直滚动,而内部
UICollectionView
水平滚动。顶级
UICollectionViewCell
s禁用焦点,并将焦点传递到内部
UICollectionView
中的项目中

Before centering vertically
-------------------
| 1xxx xxxx xxxx x|
| 1xxx xxxx xxxx x|
|                 |
| 2xxx xxxx xxxx x|
| 2xxx xxxx xxxx x|
|                 |
| 3xxx xxxx xxxx x| <-
| 3xxx xxxx xxxx x| <-
|                 |
| 4xxx xxxx xxxx x|
-------------------
它看起来像是这样的:

-------------------
|                 |
| --------------- |
| | xxxx xxxx xx| |
| | xxxx xxxx xx| |
| --------------- |
|                 |
| --------------- |
| | xxxx xxxx xx| |
| | xxxx xxxx xx| |
| --------------- |
|                 |
-------------------
我在这里试图实现的是,当焦点出现在
UICollectionView
垂直中心线下方的行上时,该行应该在
UICollectionView
中垂直居中

Before centering vertically
-------------------
| 1xxx xxxx xxxx x|
| 1xxx xxxx xxxx x|
|                 |
| 2xxx xxxx xxxx x|
| 2xxx xxxx xxxx x|
|                 |
| 3xxx xxxx xxxx x| <-
| 3xxx xxxx xxxx x| <-
|                 |
| 4xxx xxxx xxxx x|
-------------------
有人对我如何实现这一目标有何评论/建议吗

编辑:基于另一个,我尝试实现以下
UICollectionViewDelegate
功能,但它似乎对我不起作用:

func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        if let indexPath = context.nextFocusedIndexPath,
            let _ = collectionView.cellForItem(at: indexPath) {
            collectionView.contentInset.top = max((collectionView.frame.height - collectionView.contentSize.height) / 2, 0)
        }
    }
编辑:根据下面建议的答案,经过一些尝试和错误后,我已经让它使用以下代码:

func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        if let indexPath = context.nextFocusedIndexPath {
            collectionView.isScrollEnabled = false

            coordinator.addCoordinatedAnimations({
                self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
            }, completion: nil)
        }
    }

您可以像这样使用scrollToItemAtIndexPath方法:

func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    if let indexPath = context.nextFocusedIndexPath,
        let _ = collectionView.cellForItem(at: indexPath) {
        self.collectionView.scrollToItem(at: indexPath, at: 
         .centeredVertically, animated: true)
    }
}
已编辑

您将如何使用Accessibility属性或其他内容从单元格中提取indexPath,然后滚动到该indexPath,如下所示:

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    if let nextCell = context.nextFocusedView as? ScrumCollectionViewCell {
        let index = Int(nextCell.accessibilityHint!)!
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { // 2
            self.collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .centeredVertically, animated: true)
        }
    }
}

到目前为止你试过什么?@fuzz我试过这些建议,但它们似乎对我不起作用?我已经用我尝试过的示例代码编辑了我的原始问题。到底什么对你不起作用?@fuzz,啊,我的意思是,集中的行仍然没有在屏幕上居中。看看这个:谢谢你的建议,但似乎没有帮助;奇怪的是,这个单元格仍然没有垂直居中。经过一些尝试和错误,我通过扩展这个答案使它工作,我将在我的原始问题中发布我完成的代码。将此标记为答案。我也找到了解决此问题的方法,我正在编辑我的答案。