【问题标题】:How to check in UICollectionViewFlowLayout that in which direction the uicollectionview is scrolling? (Swift)如何在 UICollectionViewFlowLayout 中检查 uicollectionview 滚动的方向? (迅速)
【发布时间】:2017-04-29 04:57:03
【问题描述】:

我知道如何使用滚动视图委托方法检查方向。但不幸的是,我有一种情况,我必须在 UICollectionViewFlowLayout 中做同样的事情,但我无法使用滚动视图委托方法进行访问。 我尝试了检查(内部自定义布局)集合视图滚动方向的方法,但没有运气。在这种情况下请帮助我。

更多详情: 我想检查自定义布局内的条件。如果滚动方向是垂直的。我想知道它是顶部还是底部。

【问题讨论】:

  • 我认为您需要访问ScrollView 的委托方法,您能否解释一下,为什么您不使用scrollViewdelegate?也许您可以创建自己的自定义委托并使用自定义流程布局实现并从 scrollView 委托方法调用其方法以了解流程布局中的当前滚动方向
  • 如果我想使用scrollviewdelegate。我必须在 collectionviewlayout 中添加一个滚动视图。要添加它,每当我在布局内部执行诸如 collection.addSubView(scrollView) // 之类的操作时,它就会崩溃,我假设 colllectionview 尚未准备好,因为它位于布局内部。但我可能错了。
  • 好的,但是您可以尝试这种方式,创建您的一个协议并使用您的自定义流程布局实现并从 scrollView 委托方法调用其方法以了解流程布局中的当前滚动方向
  • 在你拥有collectionViewDatasource方法的ViewController中添加scrollView的方法,它将自动调用
  • protocol GetDirection { func scrollViewDirectionInLayout(scrollView: UIScrollView) } class CustomLayout: UICollectionViewLayout, GetDirection { func scrollViewDirectionInLayout(scrollView: UIScrollView) { //cal to find the direction } } class VC: UIViewController { func scrollViewDidScroll (scrollView: UIScrollView){ if collectionView = collectionView.collectionViewLayout as? CustomLayout { collectionView.collectionViewLayout.scrollViewDirectionInLayout(scrollView) } } } @NiravD 我想出了这个。怎么说?

标签: ios swift uiscrollview uicollectionview uicollectionviewlayout


【解决方案1】:

使用您的CustomFlowLayout 添加一个方法并使用scrollViewDidScroll 方法调用它。

class CustomFlowLayout: UICollectionViewFlowLayout {

    var currentScrollDirection = ""

    func currentScroll(direction: String) {
        currentScrollDirection = direction
    }
}

现在用ViewController 实现scrollViewDidScroll 方法,并用CustomFlowLayout 调用currentScroll 方法。

var lastContentOffset = CGPoint.zero
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let currentOffset = scrollView.contentOffset
    var scrollDirection = (currentOffset.y > self.lastContentOffset.y) ? "Down" : "Up"
    if let flowLayout = collectionView.collectionViewLayout as? CustomFlowLayout {
        flowLayout.currentScroll(direction: scrollDirection) 
    }
    self.lastContentOffset = currentOffset
}

【讨论】:

  • 我的问题可能不清楚。我会更新它。我想检查自定义布局内的条件。此外,如果滚动方向是 .vertical。我想知道它是顶部还是底部。
  • @coolly 我认为你需要访问ScrollView的委托方法你能解​​释一下为什么你不使用scrollView Delegate吗?
  • @coolly 无需protocol即可检查一个已编辑的答案
【解决方案2】:

没有任何协议或没有UIScrollViewDelegate。您可以在自定义 UICollectionViewFlow 中使用它。

枚举以获得更好的代码

enum ScrollDirection {
    case left
    case right
}

然后是你可以随时使用的功能本身

func getScrollDirection() -> ScrollDirection? {
    guard let collectionView = collectionView else { return nil }

    let scrollVelocity = collectionView.panGestureRecognizer.velocity(in: collectionView.superview)
    if (scrollVelocity.x > 0.0) {
        return .right
    } else if (scrollVelocity.x < 0.0) {
        return .left
    } else {
        return nil
    }
}

该函数在某些情况下可能返回 nil,因此请务必在需要时检查它

这样使用

if getScrollDirection() == .left

【讨论】:

    猜你喜欢
    • 2019-02-15
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多