我想實現的是一個畫廊,它可以包含多達300個圖像,使用UIScrollView
將導致非優化的內存/ CPU使用率,因爲它不會進行池化(如果我錯了),它對寬度有限制,我不能使它成爲屏幕寬度的300倍(如果我錯了,再次糾正我)。如何使表視圖捕捉到單元格中心? (用gif動畫插圖)
所以正確的選擇是使用一個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)
}
編輯: -
我設法做沒有上述鱈魚即
我想你想一個[UIPageViewController(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPageViewControllerClassReferenceClassRef/) – Fonix
爲了使水平滾動的UITableView,你必須旋轉你的UITableView和每個單元格。如果您只想通過輕掃更改1頁,則可以使用UIPageViewController。 –