2016-07-28 19 views
1

我想實現的是一個畫廊,它可以包含多達300個圖像,使用UIScrollView將導致非優化的內存/ CPU使用率,因爲它不會進行池化(如果我錯了),它對寬度有限制,我不能使它成爲屏幕寬度的300倍(如果我錯了,再次糾正我)。如何使表視圖捕捉到單元格中心? (用gif動畫插圖)

所以正確的選擇是使用一個UITableView

1 - 如何使它像在這個GIF水平滾動?

2 - 如何使它與單元格中心對齊?

gif

編輯

我使用這個,它給了我想要的結果,但它有一個問題,它對於我應該多少滑動即可移動到下一個單元格零容忍,我還需要幫助..

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

編輯: -

我設法做沒有上述鱈魚即

+0

我想你想一個[UIPageViewController(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPageViewControllerClassReferenceClassRef/) – Fonix

+0

爲了使水平滾動的UITableView,你必須旋轉你的UITableView和每個單元格。如果您只想通過輕掃更改1頁,則可以使用UIPageViewController。 –

回答

2

您應該使用UICollectionView與一個單元格或UIPageViewController來實現這種效果。沒有辦法水平使用tableview。不知何故,你可以說UICollectionView作爲UITableView的水平表示。 UIPageViewcontroller也是很好的解決方案。你應該仔細閱讀這兩者,然後決定什麼更適合你!

是的,這是不好的使用單一的滾動視圖具有不同的大小。它可以創建內存或性能問題!

更新:

你試圖啓用分頁在您的收藏視圖的滾動視圖?

看看下面的截圖

enter image description here

選中該複選框分頁啓用!

那麼之後如果沒有手感光滑的效果,那麼你應該嘗試uncheck adjust scroll view insetsviewcontroller下下,你選擇你的view controller

檢查的截圖爲

enter image description here


EDIT問問題的人: -

添加此工作適合我,因爲UICollectionView沒有 adjust scroll view insets選項。

layout.minimumInteritemSpacing = 0 
layout.minimumLineSpacing = 0 

+0

謝謝,我知道如何使用UICollectionView,我會閱讀關於UIPageViewcontroller,並看看會發生什麼。 – DeyaEldeen

+0

查看我的回覆更新! – Lion

+0

如何以編程方式取消選中[調整滾動視圖插圖],我可以將其應用於self.view,但不適用於集合視圖? – DeyaEldeen

0

可以使用UICollectionView和配置集合觀察室

集collectionViewLayout大小...這個尺寸基於上述觀點

- (CGSize)的CollectionView:(UICollectionView *)的CollectionView佈局:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {return} CGSizeMake(250,150); }

相關問題