【发布时间】:2016-07-03 02:18:55
【问题描述】:
我已经建立了一个collectionView,我想主要在视图的中心显示一张电影海报。我将在 collectionView 中显示大约 20 部电影,所以我想在右边缘显示下一部电影的一小部分,当我移至下一页时,焦点中的电影将居中,前一个和下一部电影分别在左侧和右侧伸出。
按照我的代码方式,当我翻到下一部电影时,下一部电影根本没有居中,它在 x 轴上向右移动了太多。如何以编程方式设置分页所需的距离?如何实现 collectionView 在第一个页面之后正确居中页面的能力?
在我的 collectionView 上启用了分页。我还没有创建自定义 collectionViewFlowLayout。
Screenshots of my issue Video of my issue
这是我的 collectionView 和 scrollView 代码:
extension MainViewController: UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let posterWidth = (collectionView.frame.size.height / 3) * 2
return CGSizeMake(posterWidth, collectionView.frame.size.height)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
let posterWidth = (collectionView.frame.size.height / 3) * 2
let horizontalInset = (collectionView.frame.size.width - posterWidth) / 2
return UIEdgeInsetsMake(0, horizontalInset, 0, horizontalInset)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 20
}
}
---------------
extension MainViewController: UIScrollViewDelegate {
func scrollViewDidScroll(scrollView: UIScrollView) {
let index = Int(collectionView.contentOffset.x / collectionView.frame.size.width)
let movie = movies[index]
updateViews(movie)
}
}
【问题讨论】:
标签: swift uiscrollview pagination uicollectionview uicollectionviewcell