我想要实现的非常简单:每次用户聚焦一个集合视图单元格时,我想让焦点单元格水平居中.似乎我目前的方法根本不起作用.
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)
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,集合视图忽略了任何在任何地方滚动的尝试.出于测试目的,我将索引路径更改为集合视图中的最后一个项目的索引路径,但它仅在首次显示集合视图时有效.
小智 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)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2823 次 |
| 最近记录: |