tvos - 如何将聚焦的 UICollectionViewCell 居中?

标签 tvos apple-tv

我想要实现的很简单:每次用户聚焦一个集合 View 单元格时,我想让聚焦的单元格水平居中。看来我目前的方法根本不起作用。

    func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        guard collectionView === self.categoryCollectionView else {
            return
        }
        guard let indexPath = context.nextFocusedIndexPath else {
            return
        }

        collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .CenteredHorizontally, animated: true)
    }

奇怪的是,集合 View 忽略了在任何地方滚动的任何尝试。出于测试目的,我将索引路径更改为集合 View 中最后一项的索引路径,但它仅在集合 View 第一次出现时才有效。

最佳答案

诀窍是确保您已设置 UICollectionView scrollEnabled = false。

override func didUpdateFocusInContext(context: UIFocusUpdateContext, 
  withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    if let focusedView = context.nextFocusedView as? UITableViewCell {
      collectionView.scrollEnabled = false
      let indexPath = collectionView.indexPathForCell(focusedView)!
      collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .CenteredHorizontally, animated: true)
    }
}

关于tvos - 如何将聚焦的 UICollectionViewCell 居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32991933/

相关文章:

ios - 想要使用 ios sdk 中的 AirPlay 应用程序在 Apple TV 上流式传输我的应用程序 View ?

ios - 是否仍然可以在 iOS 和 tvOS 之间共享 UserDefaults?

swift - 如何在 tvOS 中获得焦点 View 帧大小?

ios - View 内的 AVPlayerViewController 未显示播放控件

ios - Xcode 10.3 中缺少模拟器

swift - 使用 tvOS 强制触摸

ios - Apple TV - 如何下载和安装 "ipa"文件?

swift - Apple TV 上的多人游戏

uitextfield - tvOS UITextField 编辑后空白

swift - tvos:UIViewController 没有收到 touchesBegan