如何使表格视图对齐单元格中心? (附gif动图)
How to make a tableview snap to cells centers ? (with gif animation illustration)
我想要实现的是一个画廊,它最多可以包含 300 张图像,使用 UIScrollView
将导致未优化的 ram/cpu 使用,因为它不进行池化(正确如果我错了我),并且它有宽度限制,我不能使它成为屏幕宽度的300倍(如果我错了再次纠正我)。
所以正确的选择是使用 UITableView
1 - 如何让它像这个 gif 中那样水平滚动?
2 - 如何使其对齐单元格中心?
编辑
我正在使用它,它给出了我想要的结果但它有一个问题,它不能容忍我应该滑动多少才能移动到下一个单元格,我仍然需要帮助..
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
// Find collectionview cell nearest to the center of collectionView
// Arbitrarily start with the last cell (as a default)
var closestCell : UICollectionViewCell = collectionView.visibleCells()[0];
for cell in collectionView!.visibleCells() as [UICollectionViewCell]
{
let closestCellDelta = abs(closestCell.center.x - collectionView.bounds.size.width/2.0)
let cellDelta = abs(cell.center.x - collectionView.bounds.size.width/2.0)
if (cellDelta < closestCellDelta)
{
closestCell = cell
}
}
let indexPath = collectionView.indexPathForCell(closestCell)
collectionView.scrollToItemAtIndexPath(indexPath!, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
}
func scrollViewWillBeginDecelerating(scrollView: UIScrollView)
{
// Find collectionview cell nearest to the center of collectionView
// Arbitrarily start with the last cell (as a default)
var closestCell : UICollectionViewCell = collectionView.visibleCells()[0];
for cell in collectionView!.visibleCells() as [UICollectionViewCell]
{
let closestCellDelta = abs(closestCell.center.x - collectionView.bounds.size.width/2.0)
let cellDelta = abs(cell.center.x - collectionView.bounds.size.width/2.0)
if (cellDelta < closestCellDelta)
{
closestCell = cell
}
}
let indexPath = collectionView.indexPathForCell(closestCell)
collectionView.scrollToItemAtIndexPath(indexPath!, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
}
编辑:-
我没有上面的代码就成功了。
你应该使用UICollectionView with one cell or UIPageViewController来实现这种效果。没有办法横向使用tableview。你可以用某种方式说 UICollectionView
作为 UITableView
的水平表示。 UIPageViewcontroller
也是很好的解决方案。您应该详细阅读两者,然后再决定哪一个更适合您!
是的,使用尺寸如此不同的单个滚动视图并不好。它会造成内存或性能问题!!
更新:
您是否尝试过在集合视图的滚动视图中启用分页?
看下面的截图
选中启用分页复选框!
然后如果你觉得效果不流畅那么你应该尝试在viewcontroller
下uncheck adjust scroll view insets
在attribute inspector
下你选择你的view controller
查看屏幕截图
编辑 提问者:-
添加这个对我有用,因为 UICollectionView
没有
adjust scroll view insets
选项。
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
您可以使用 UICollectionView 并配置集合视图单元格
设置 collectionViewLayout 大小...此大小基于上面的视图
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(250, 150);
}
我想要实现的是一个画廊,它最多可以包含 300 张图像,使用 UIScrollView
将导致未优化的 ram/cpu 使用,因为它不进行池化(正确如果我错了我),并且它有宽度限制,我不能使它成为屏幕宽度的300倍(如果我错了再次纠正我)。
所以正确的选择是使用 UITableView
1 - 如何让它像这个 gif 中那样水平滚动?
2 - 如何使其对齐单元格中心?
编辑
我正在使用它,它给出了我想要的结果但它有一个问题,它不能容忍我应该滑动多少才能移动到下一个单元格,我仍然需要帮助..
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
// Find collectionview cell nearest to the center of collectionView
// Arbitrarily start with the last cell (as a default)
var closestCell : UICollectionViewCell = collectionView.visibleCells()[0];
for cell in collectionView!.visibleCells() as [UICollectionViewCell]
{
let closestCellDelta = abs(closestCell.center.x - collectionView.bounds.size.width/2.0)
let cellDelta = abs(cell.center.x - collectionView.bounds.size.width/2.0)
if (cellDelta < closestCellDelta)
{
closestCell = cell
}
}
let indexPath = collectionView.indexPathForCell(closestCell)
collectionView.scrollToItemAtIndexPath(indexPath!, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
}
func scrollViewWillBeginDecelerating(scrollView: UIScrollView)
{
// Find collectionview cell nearest to the center of collectionView
// Arbitrarily start with the last cell (as a default)
var closestCell : UICollectionViewCell = collectionView.visibleCells()[0];
for cell in collectionView!.visibleCells() as [UICollectionViewCell]
{
let closestCellDelta = abs(closestCell.center.x - collectionView.bounds.size.width/2.0)
let cellDelta = abs(cell.center.x - collectionView.bounds.size.width/2.0)
if (cellDelta < closestCellDelta)
{
closestCell = cell
}
}
let indexPath = collectionView.indexPathForCell(closestCell)
collectionView.scrollToItemAtIndexPath(indexPath!, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
}
编辑:-
我没有上面的代码就成功了。
你应该使用UICollectionView with one cell or UIPageViewController来实现这种效果。没有办法横向使用tableview。你可以用某种方式说 UICollectionView
作为 UITableView
的水平表示。 UIPageViewcontroller
也是很好的解决方案。您应该详细阅读两者,然后再决定哪一个更适合您!
是的,使用尺寸如此不同的单个滚动视图并不好。它会造成内存或性能问题!!
更新:
您是否尝试过在集合视图的滚动视图中启用分页?
看下面的截图
选中启用分页复选框!
然后如果你觉得效果不流畅那么你应该尝试在viewcontroller
下uncheck adjust scroll view insets
在attribute inspector
下你选择你的view controller
查看屏幕截图
编辑 提问者:-
添加这个对我有用,因为 UICollectionView
没有
adjust scroll view insets
选项。
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
您可以使用 UICollectionView 并配置集合视图单元格
设置 collectionViewLayout 大小...此大小基于上面的视图
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(250, 150); }