【发布时间】:2020-11-01 06:28:37
【问题描述】:
我有一个UICollectionView,我的问题是,如果我在集合视图滚动仍在进行时关闭包含集合视图的视图控制器,则集合视图不会被剪裁到边缘视图,当视图控制器被关闭时,我可以看到视图边界之外的单元格。
我正在使用集合视图流布局,这是我的代码:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
let height = collectionView.frame.height - flow.sectionInset.top - flow.sectionInset.bottom
let size = CGSize(width: cellWidth, height: height)
return size
}
我也用下面的代码来操作滚动过程,所以每次滚动都会在指定的点停止:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let width: CGFloat = cellWidth + cellOffset
let xOffset = scrollView.contentOffset.x
let page: Double = Double(xOffset / width)
var pageInt: Int = Int(page.round(to: 0))
if velocity != .zero {
pageInt = velocity.x > 0 ? pageInt+1 : pageInt-1
}
let newXOffset = CGFloat(pageInt) * width
let point = CGPoint(x: newXOffset, y: 0)
targetContentOffset.pointee = point
}
【问题讨论】:
标签: ios swift xcode uicollectionview uiscrollview