我在一个单元格中有一个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
}
}
}
ParentCell:
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)
}
}
ChildCell:
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)
}
}
答案 0 :(得分:4)
您可以使用以下方式强制抵消:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == myNotScrollingView {
//you will never bounce to left
scrollView.contentOffset.x = max(0,scrollView.contentOffset.x)
}
}
答案 1 :(得分:1)
喜欢
// create one global float value for get the every scroll
var previousX: Float = 0.0
为您的父级和子级集合视图设置标记,然后根据标记进行访问
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x < previousX {
if scrollView.tag == 1 // set your tag
{
scrollView.contentOffset = CGPoint(x: CGFloat(previousX), y: CGFloat(scrollView.contentOffset.y))
}
else {
previousX = scrollView.contentOffset.x
}
}
}
<强> UPADTE 强>
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x < previousX {
var cell: UICollectionViewCell? = (UICollectionView.viewwithtag(1) as? UICollectionViewCell)
if cell.tag == 1 // set your tag
{
scrollView.contentOffset = CGPoint(x: CGFloat(previousX), y: CGFloat(scrollView.contentOffset.y))
}
else {
previousX = scrollView.contentOffset.x
}
}
}
upadte 2
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x < previousX {
var cell: UICollectionViewCell? = (UICollectionView.viewwithtag(1) as? UICollectionViewCell)
if (cell is ChildCell)
{
scrollView.contentOffset = CGPoint(x: CGFloat(previousX), y: CGFloat(scrollView.contentOffset.y))
}
else {
previousX = scrollView.contentOffset.x
}
}
}
else if (cell is ParentCell) {
//Another custom cell
}
else {
//General cell
}
}