【问题标题】:tvOS UICollectionView get currently focused item's index pathtvOS UICollectionView 获取当前焦点项目的索引路径
【发布时间】:2015-09-26 20:26:53
【问题描述】:

所以这是一个很无聊的问题,但我只是想不出一种非常简单的方法来检测当前关注的项目的indexPath
我环顾四周,希望能看到像collectionView.indexPathOfCurrentlyFocusedItem 这样非常简单的东西,但没有找到任何离得很近的东西。 所以我四处挖掘并试图在UIFocusEnvironmentUIFocusUpdateContext 找到类似的东西,试图找到所需的属性但失败了。 因此,我能想出的唯一解决方案就是遍历所有可见单元格并找到一个具有焦点属性设置为 true 的单元格。

那么有没有更简单优雅的方法来查找当前关注的项目的indexPath(除了通过委托方法跟踪并保存在视图控制器的属性中)

【问题讨论】:

    标签: uicollectionview tvos


    【解决方案1】:

    您可以使用UIScreen 属性focusedView 如下:

    if let focusedCell = UIScreen.main.focusedView as? UICollectionViewCell {
        if let indexPath = collectionView.indexPath(for: focusedCell) {
            print("IndexPath is \(indexPath)")
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用didUpdateFocusInContect - UICollectionViewDelegate

      func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
          if collectionView == self.collectionView {
              print(context.nextFocusedIndexPath)
          }
      }
      

      这将返回要聚焦的单元格的 indexPath,您也可以尝试:

      context.previouslyFocusedIndexPath
      

      取决于你想要做什么。

      【讨论】:

      • 感谢您的回答,我自己想出了这个。但这需要我将 nextFocusedIndexPath 存储在一个属性中,而我想要更方便的东西,比如collectionView.indexPathOfCurrentlyFocusedItem
      • 这个可能的焦点单元格对于特定的索引路径,即我们提供硬编码的索引路径
      【解决方案3】:

      因此,您的目标是在您特别关注 tvOS 下的单元时做某事。问题是您正在移动其他 UI 元素,因此上下文可能会有所不同。在这种情况下,您必须只更改您必须关心的那些 UI。

      实现你的正确位置是 func didUpdateFocusInContext(),像这样:

      override func didUpdateFocusInContext(
        context:                              UIFocusUpdateContext,
        withAnimationCoordinator coordinator: UIFocusAnimationCoordinator
      ) {
        coordinator.addCoordinatedAnimations({
          if let cell = context.previouslyFocusedView as? UICollectionViewCell {
            cell.layer.borderWidth = 2
          }
      
          if let cell = context.nextFocusedView as? UICollectionViewCell {
            cell.layer.borderWidth = 5
          }
        },
        completion: nil)
      }
      

      现在我们使用焦点协调器来应用我们的逻辑:

      • 当先前聚焦的项目是 UICollectionViewCell 时,您必须将焦点释放到下一个项目。您不应该关心下一个项目是什么,因为它可能是一个集合单元格,也可能不是。为了好玩,在这种情况下,让我们将边框更改为 2。这个值可以默认设置。
      • 当下一个焦点项是 UICollectionViewCell 时,您必须以类似的方式处理它,否则会变得一团糟……所以,让我们将边框更改为 5。

      如您所见,didUpdateFocusInContext() 为当前视觉上下文中的所有视图提供了一种通用方法。您可以对其他 UI 元素应用相同的方法。

      享受 tvOS 带来的乐趣...

      【讨论】:

        【解决方案4】:

        这是我在 shouldUpdateFocusInContext 中完成此操作的方法

        解决方案

        override func shouldUpdateFocusInContext(context: UIFocusUpdateContext) -> Bool {
        
            // The magic is in the next two lines
            let cell: UICollectionViewCell = context.nextFocusedView as! UICollectionViewCell
            let indexPath: NSIndexPath? = self.collectionView.indexPathForCell(cell)
        
            print(indexPath) 
            // <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
            
            return true
        }
        

        【讨论】:

        • 工作就像一个魅力。 +1
        猜你喜欢
        • 1970-01-01
        • 2014-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多