最近開發(fā)產(chǎn)品需要寫一個小功能锈遥,類似淘寶商品詳情頁下拉刷新會以卡片式顯示之前瀏覽過的商品纫事,在這之前,自己先嘗試著寫了一個小demo,話不多說所灸,先看圖:
功能很簡單丽惶,橫向滾動圖片,始終讓當(dāng)前視圖中的一張圖片居中爬立,控件采用UICollectionView流式布局
(具體方法實現(xiàn)代碼請參照github上的代碼)
1.首先需要重寫UICollectionViewFlowLayout的方法钾唬,創(chuàng)建CDFlowLayout繼承UICollectionViewFlowLayout
重寫
```
override func targetContentOffset(forProposedContentOffset proposedContentOffset:CGPoint, withScrollingVelocity velocity:CGPoint) ->CGPoint?
override func layoutAttributesForElements(in rect:CGRect) -> [UICollectionViewLayoutAttributes]?
override func shouldInvalidateLayout(forBoundsChange newBounds:CGRect) ->Bool
```
2. 創(chuàng)建cell,在cell視圖上添加UIImageView視圖
3.在ViewController中創(chuàng)建UICollectionView