您的当前位置:首页正文

swift写的卡片式滚动浏览图片

来源:华拓网

最近开发产品需要写一个小功能,类似淘宝商品详情页下拉刷新会以卡片式显示之前浏览过的商品,在这之前,自己先尝试着写了一个小demo,话不多说,先看图:

功能很简单,横向滚动图片,始终让当前视图中的一张图片居中,控件采用UICollectionView流式布局

(具体方法实现代码请参照github上的代码)

1.首先需要重写UICollectionViewFlowLayout的方法,创建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. 创建cell,在cell视图上添加UIImageView视图

3.在ViewController中创建UICollectionView