Ios 如何在UICollectionViewFlowLayout中检查uicollectionview的滚动方向?(Swift)

Ios 如何在UICollectionViewFlowLayout中检查uicollectionview的滚动方向?(Swift),ios,swift,uiscrollview,uicollectionview,uicollectionviewlayout,Ios,Swift,Uiscrollview,Uicollectionview,Uicollectionviewlayout,我知道如何使用滚动视图委托方法检查方向。但不幸的是,我有一种情况,我必须在UICollectionViewFlowLayout中执行相同的操作,但我无法使用scroll view委托方法进行访问。 我尝试了检查(在自定义布局内)集合视图滚动方向的方法,但没有成功。请帮我解决这个问题 更多详情: 我想检查自定义布局内部的条件。如果滚动方向是垂直的。我想找到它是顶部还是底部。使用CustomFlowLayout添加一个方法,并使用scrollViewDidScroll方法调用它 class Cust

我知道如何使用滚动视图委托方法检查方向。但不幸的是,我有一种情况,我必须在UICollectionViewFlowLayout中执行相同的操作,但我无法使用scroll view委托方法进行访问。 我尝试了检查(在自定义布局内)集合视图滚动方向的方法,但没有成功。请帮我解决这个问题

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

使用
CustomFlowLayout
添加一个方法,并使用
scrollViewDidScroll
方法调用它

class CustomFlowLayout: UICollectionViewFlowLayout {

    var currentScrollDirection = ""

    func currentScroll(direction: String) {
        currentScrollDirection = direction
    }
}
现在用
ViewController
实现
scrollviewdicscroll
方法,并用
CustomFlowLayout
调用
currentcoll
方法

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
}

使用
CustomFlowLayout
添加一个方法,并使用
scrollViewDidScroll
方法调用它

class CustomFlowLayout: UICollectionViewFlowLayout {

    var currentScrollDirection = ""

    func currentScroll(direction: String) {
        currentScrollDirection = direction
    }
}
现在用
ViewController
实现
scrollviewdicscroll
方法,并用
CustomFlowLayout
调用
currentcoll
方法

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
}

没有任何协议或没有
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
    }
}

没有任何协议或没有
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
    }
}

我的问题可能不清楚,也可能不清楚。我会更新的。我想检查自定义布局内部的条件。另外,如果滚动方向是垂直的。我想找到它是在顶部还是在底部。@冷静地说,我认为你需要访问
ScrollView
的委托方法,你能解释一下为什么不使用ScrollView委托吗?@冷静地检查一个编辑过的答案,而不需要
协议
我的问题可能不清楚,可能是。我会更新的。我想检查自定义布局内部的条件。另外,如果滚动方向是垂直的。我想找到它是在顶部还是在底部。@冷静地说,我认为你需要访问
ScrollView
的委托方法,你能解释一下为什么不使用ScrollView委托吗?@冷静地检查一个编辑过的答案,而不需要
协议
我认为你需要访问
ScrollView
,请您详细解释一下,为什么不使用scrollViewdelegate?如果我想使用scrollviewdelegate,您可以创建自己的自定义委托并使用自定义flowlayout实现,并从scrollView委托方法调用其方法以了解flowlayout中的当前滚动方向。我必须在collectionviewlayout中添加一个scrollview。要添加这一点,每当我在布局内部执行类似于collection.addSubView(scrollView)//的操作时,它都会崩溃,我假设CollectionView未准备就绪,因为它位于布局内部。但我可能错了。好吧,但你可以这样试试,创建一个协议,并使用自定义flowlayout实现,并从scrollView委托方法调用其方法,以了解flowlayoutIn ViewController中当前的滚动方向,在该视图控制器中,您将使用collectionViewDatasource方法添加scrollView的方法,该方法将自动调用protocol GetDirection{func-ScrollViewDirectionLayout(scrollView:UIScrollView)}类CustomLayout:UICollectionViewLayout,GetDirection{func-ScrollViewDirectionLayout(scrollView:UIScrollView){//cal查找方向}类VC:UIViewController{func-scrollViewDidScroll(scrollView:UIScrollView){如果collectionView=collectionView.collectionViewLayout为?CustomLayout{collectionView.collectionViewLayout.ScrollViewDirectionLayout(scrollView)}}@NiravD我想到了这个。你说呢?我想你需要访问
ScrollView
的委托方法,你能解释一下吗,为什么你不使用scrollViewdelegate?也许你可以创建你自己的自定义委托,并用自定义flowlayout实现,并从ScrollView委托方法调用它的方法来知道FlowLayout中的当前滚动方向如果我想使用scrollviewdelegate。我必须在collectionviewlayout中添加一个scrollview。每当我执行类似于将inside layout作为collection的操作时,都要添加它。addSubView(scrollview)//它崩溃了,我假设CollectionView没有准备好,因为它在布局中。但我可能错了。好的,但您可以尝试这种方法,创建一个协议,用自定义flowlayout实现,并从scrollView委托方法调用它的方法,以了解flowlayoutIn ViewController中当前的滚动方向,您在其中使用cCollectionViewDataSource方法以及它添加了scrollView的方法,它将自动调用协议GetDirection{func ScrollViewDirectionLayout(scrollView:UIScrollView)}类CustomLayout:UICollectionViewLayout,GetDirection{func ScrollViewDirectionLayout(scrollView:UIScrollView){//cal查找方向}类VC:UIViewController{func scrollViewDidScroll(scrollView:UIScrollView){如果collectionView=collectionView.collectionViewLayout as?CustomLayout{collectionView.collectionViewLayout.ScrollViewDirectionLayout(scrollView)}@NiravD我想到了这个,你说呢?