【发布时间】:2017-03-03 09:07:05
【问题描述】:
我在一个单元格中有一个 collectionView。 collectionView 在水平方向滑动。
在collectionView中我调用了这个函数:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = scrollView.contentOffset.x
print(offset)
}
当偏移量为 0 时,用户无需向左滑动,因为在该方向上看不到任何东西。
当偏移量为 0 时,如何暂时禁用单向滚动?在这种情况下,在向右滚动时禁用向左滚动仍然处于启用状态。
编辑:我忘了说,带有 collectionView 的单元格位于 collectionView 的另一个单元格内,这两个单元格都水平滚动。我想在子单元格上禁用滚动并同时在父单元格上启用滚动。所以,当子单元格内的collectionView水平滚动不再启用时,父单元格开始滚动。
class ViewController: UIViewController {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.delegate = self
cv.dataSource = self
cv.isPagingEnabled = true
return cv
}()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
// Other cell here to swipe to when user cant swipe collectionView inside child cell.
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ParentCell.reuseIdentifier, for: indexPath) as! ParentCell
return cell
}
}
}
父单元:
class ParentCell: UICollectionViewCell {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.delegate = self
cv.dataSource = self
cv.isPagingEnabled = true
cv.showsHorizontalScrollIndicator = false
return cv
}()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ChildCell.reuseIdentifier, for: indexPath) as! ChildCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
}
子细胞:
class ChildCell: UICollectionViewCell {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.delegate = self
cv.dataSource = self
cv.tag = 1
return cv
}()
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = scrollView.contentOffset.x
print(offset)
}
}
【问题讨论】: