4

私が達成したいことは非常に単純です。ユーザーがコレクションビューセルにフォーカスするたびに、フォーカスされたセルを水平方向に中央揃えにしたいと考えています。私の現在のアプローチはまったく機能していないようです。

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

奇妙なのは、コレクション ビューがどこにスクロールしようとしても無視されることです。テスト目的で、インデックス パスをコレクション ビューの最後の項目のインデックス パスに変更しましたが、コレクション ビューが初めて表示されたときにのみ機能します。

4

2 に答える 2

8

トリックは、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)
    }
}
于 2015-12-29T05:43:40.697 に答える
0

AirBnB の Focus Engine ガイドのどこかで、彼らがこれについて多少説明していたのを読んだことを覚えています。彼らのコード ( GitHub ) は UIButton クラス用ですが、そこから CollectionView の抽象を描画できることを願っています。

解決策を見つけるのに役立つことを願っています!

于 2015-10-09T14:19:12.550 に答える