2017-04-10 61 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 

}