我有一個我UICollectionView在其中顯示一個UICollectionViewCell. 每個單元有相同height和width作為collection view。
我想限制用戶UICollectionViewCell在進入下一個單元格后滑回
有點像火種體驗,雖然我Pagination enabled在我的UICollectionView
uj5u.com熱心網友回復:
您可以通過以下步驟執行此操作
您必須在滾動結束后獲取當前行并更新您的當前索引
currentIndex
物件然后允許視圖滾動或不滾動。確保您已經正確計算了左右邊緣的單元格寬度和集合視圖的單元格空間
var currentIndex = 0
var cellWidth = 0.0
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let row = scrollView.contentOffset.x / cellWidth
currentIndex = Int(row)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x < cellWidth * CGFloat(currentIndex){
scrollView.contentOffset = CGPoint(x: cellWidth * CGFloat(currentIndex), y: -20)
scrollView.bounces = false
} else {
scrollView.bounces = true
}
}
uj5u.com熱心網友回復:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let indexPathOfVisible = collectionView.indexPath(for: collectionView.visibleCells.first ?? UICollectionViewCell())
if indexPath.row < indexPathOfVisible?.row ?? 0 {
collectionView.scrollToItem(at: indexPathOfVisible!, at: .right, animated: false)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/338364.html
標籤:ios 迅速 uicollectionview uicollectionviewcell uicollectionviewlayout
