有人可以帮我吗? 我有带有插图的收藏夹视图,水平滚动方向,启用分页功能,并且单元格之间没有空间。问题是分页不会在每个单元格上停止。
我尝试了所有可以在StackOverflow和Internet上找到的解决方案,但是没有任何效果。我使用Xcode 10和Swift5,这是用于iPhone应用程序的。我没有添加我的collectionviewcell代码,因为它只有Label的IBoutlet。
class ViewController: UIViewController, UICollectionViewDataSource,
UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
private let item = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday", "Sunday"]
private let cellScaling: CGFloat = 0.33
private var cellWidth: CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
UpdateUI()
}
func UpdateUI(){
self.collectionView.backgroundColor = UIColor.white
let screenSize = UIScreen.main.bounds.size
let cellWidth = floor(screenSize.width * cellScaling)
let insetX = (view.bounds.width - cellWidth) / 2
let layout = collectionView!.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSize(width: cellWidth, height: collectionView.bounds.height)
collectionView.contentInset = UIEdgeInsets(top: 0, left: insetX, bottom: 0, right: insetX)
// layout.minimumLineSpacing = 0
self.cellWidth = cellWidth
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.item.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.label.text = self.item[indexPath.row]
cell.backgroundColor = UIColor.cyan
return cell
}
private var startingScrollingOffset = CGPoint.zero
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
startingScrollingOffset = scrollView.contentOffset
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let page: CGFloat
let offset = scrollView.contentOffset.x + scrollView.contentInset.left
let proposedPage = offset / max(1, cellWidth)
let snapPoint: CGFloat = 0.1
let snapDelta: CGFloat = offset > startingScrollingOffset.x ? (1-snapPoint) : snapPoint
if floor(proposedPage + snapDelta) == floor(proposedPage){
page = floor(proposedPage)
} else {
page = floor(proposedPage + 1)
}
targetContentOffset.pointee = CGPoint(x: cellWidth * page, y: targetContentOffset.pointee.y)
}
}
请注意,我尝试了所有其他可以找到的解决方案。在您投票删除该问题之前。该照片显示了每个单元格的外观。
答案 0 :(得分:0)
几个月前,我不得不做一个类似的收藏视图, 这是我使用的代码:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let layout = theNameOfYourCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
let cellWidthIncludingSpacing = layout.itemSize.width + layout.minimumLineSpacing // Calculate cell size
let offset = scrollView.contentOffset.x
let index = (offset + scrollView.contentInset.left) / cellWidthIncludingSpacing // Calculate the cell need to be center
if velocity.x > 0 { // Scroll to -->
targetContentOffset.pointee = CGPoint(x: ceil(index) * cellWidthIncludingSpacing - scrollView.contentInset.right, y: -scrollView.contentInset.top)
} else if velocity.x < 0 { // Scroll to <---
targetContentOffset.pointee = CGPoint(x: floor(index) * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)
} else if velocity.x == 0 { // No dragging
targetContentOffset.pointee = CGPoint(x: round(index) * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)
}
}
此代码计算单元格的大小,已经显示了多少个单元格,滚动完成后,对其进行调整以使单元格居中。
如果要使用此代码,请确保您在pagingEnabled
中具有collectionView的false
。
希望我能帮助您!