2017-04-10 42 views
0

我想建立一個像這樣的一個集合視圖: Collection View的iOS夫特3:UICollectionView水平中心和大細胞

其在中心處具有更大的細胞和細胞捕捉到容器視圖的中心,但與夫特3.我不想使用庫,因爲我想學習如何構建像這樣的自定義集合視圖。

我搜索了SO,但沒有找到任何合適的解決方案尚未

+2

嗨,請檢查鏈接,投票,如果它幫助。 https://github.com/ink-spot/UPCarouselFlowLayout –

+0

這一個很好,我怎麼接受這個答案? –

+0

只需按下上面的Flag按鈕即可投票,您可以通過將指針懸停在我的答案上找到兩者。謝謝。 –

回答

0

寫的是功能

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height) 
    } 

化妝集合視圖[滾動方向]水平

和[滾動]蜱滾動啓用和尋呼啓用

make cell biger

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 

    let offSet = self.collectionView.contentOffset 
    let width = self.collectionView.bounds.size.width 

    let index = round(offSet.x/width) 
    let newPoint = CGPoint(x: index * size.width, y: offSet.y) 


    coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) in 

    },completion: {(UIVIewTransitionCoordinatorContext) in 
     self.collectionView.reloadData() 
     self.collectionView.setContentOffset(newPoint, animated: true) 
    }) 

} 
+0

如何使中心單元比左右單元更大? –

0

要做到這一點,你將需要繼承UICollectionViewFlowLayout並重寫:

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath 

然後調用super.layoutAt ...並改變它通過其.transform屬性返回小區,並返回你的改變屬性

這是我以前做的一個例子。

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 

var att = super.layoutAttributesForElements(in: rect)! 

if !(delegate?.reelPlayerFlowIsPreviewMode() ?? true) { 
    return att 
} 
let region = CGRect(x: (self.collectionView?.contentOffset.x)!, 
        y: (self.collectionView?.contentOffset.y)!, 
        width: (self.collectionView?.bounds.size.width)!, 
        height: (self.collectionView?.bounds.size.height)!) 

let center = CGPoint(x: region.midX, y: region.midY) 

for theCell in att { 

    print("\(theCell.indexPath.item)\n\(theCell)\n") 
    let cell = theCell.copy() as! UICollectionViewLayoutAttributes 
    var f = cell.frame 
    let cellCenter = CGPoint(x: f.midX, y: f.midY) 
    let realDistance = min(center.x - cellCenter.x, region.width) 
    let distance = abs(realDistance) 
    let d = (region.width - distance)/region.width 
    let p = (max(d, ReelPlayerFlowLayout.minPercent) * ReelPlayerFlowLayout.maxPercent) 

    f.origin.x += (realDistance * ((1 - ReelPlayerFlowLayout.maxPercent) + (ReelPlayerFlowLayout.maxPercent - ReelPlayerFlowLayout.minPercent))) 

    cell.frame = f 
    cell.size = CGSize (width: f.width * p, height: f.height * p)    
    let index = att.index(of: theCell)! 
    att[index] = cell 
} 

return att 

}