2014-06-19 61 views
4

我必須創建網格視圖像Appstore iOS應用程序。我想用UICollectionView分頁來做到這一點。我也實現了代碼,但不能像這樣滾動。UICollectionView與分頁啓用

我想要做的是在中心和兩側(左側和右側)會有一張圖片,它應該顯示上一張和下一張圖片的一部分。我爲UICollectionView設置了Frame爲320 * 320。單元格大小爲290 * 320(單元格最小間距爲10)1

下面是描述我的要求的兩個鏈接。提前致謝。

(這是我想要的)2

回答

3

如果您在使用的CollectionView它pagining將由一個頁面沒有一個細胞滾動。您可以禁用pagining和實施ScrollViewDelegate

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset 
{ 
    CGFloat pageW = 300; 
    int page = scrollView.contentOffset.x/pageW; 

    CGFloat newOffset =(page + ((velocity.x > 0)? 1 : -1)) * (pageW - 20); 
    CGPoint target = CGPointMake(newOffset, 0); 
    targetContentOffset = ⌖ 
    NSLog(@"end Drag at %f /%i /%f",scrollView.contentOffset.x, page, velocity.x); 

} 

只有一個從非標準分頁不同:如果將快速收集將滾動不止一個小區。 別忘了添加UIScrollViewDelegate

+0

還是同樣的問題.......我必須設置什麼設置targetContentOffset後? – user3755698

+0

您是否爲collectionView設置了pagingEnabled = NO? – shtefane

+0

是的,我已將它設置爲NO – user3755698

9

您是否嘗試將UICollectionViewFlowLayout的滾動方向設置爲水平?

[yourFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

您需要在您的收藏視圖啓用分頁,像這樣:

[yourCollectionView setPagingEnabled:YES];

+1

對於那些使用故事板的人,您還可以在集合視圖的屬性檢查器中找到Interface Builder中啓用的複選框。 – Bocaxica

6

我把shtefane's answer並對其改進。輸入您自己的cellWidthcellPadding值。

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView 
        withVelocity:(CGPoint)velocity 
       targetContentOffset:(inout CGPoint *)targetContentOffset 
{ 
    CGFloat cellWidth = self.cellWidth; 
    CGFloat cellPadding = 9; 

    NSInteger page = (scrollView.contentOffset.x - cellWidth/2)/(cellWidth + cellPadding) + 1; 

    if (velocity.x > 0) page++; 
    if (velocity.x < 0) page--; 
    page = MAX(page,0); 

    CGFloat newOffset = page * (cellWidth + cellPadding); 
    targetContentOffset->x = newOffset; 
} 
0

裁判Rickster的答案,我重寫與斯威夫特4:

/* paging */ 
extension AlbumViewController { 

    /* In case the user scrolls for a long swipe, the scroll view should animate to the nearest page when the scrollview decelerated. */ 
    override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 
     scrollToPage(scrollView, withVelocity: CGPoint(x:0, y:0)) 
    } 

    override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { 
     scrollToPage(scrollView, withVelocity: velocity) 
    } 

    func scrollToPage(_ scrollView: UIScrollView, withVelocity velocity: CGPoint) { 
     let cellWidth: CGFloat = cellSize 
     let cellPadding: CGFloat = 10 

     var page: Int = Int((scrollView.contentOffset.x - cellWidth/2)/(cellWidth + cellPadding) + 1) 
     if velocity.x > 0 { 
      page += 1 
     } 
     if velocity.x < 0 { 
      page -= 1 
     } 
     page = max(page, 0) 
     let newOffset: CGFloat = CGFloat(page) * (cellWidth + cellPadding) 

     scrollView.setContentOffset(CGPoint(x:newOffset, y:0), animated: true) 
    } 
} 
0

這裏的Rickster答案的工作swift4版本:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { 

    let cellWidth = 174 as CGFloat 
    let cellPadding = 10 as CGFloat 

    var page = (scrollView.contentOffset.x - cellWidth/2)/(cellWidth + cellPadding) + 1 

    if (velocity.x > 0) { page += 1 } 
    if (velocity.x < 0) { page -= 1 } 

    page = max(page,0) 

    targetContentOffset.pointee.x = page * (cellWidth + cellPadding) 
}