如何在 UICollectionViewFlowLayout 中检查 uicollectionview 滚动的方向? (Swift)
How to check in UICollectionViewFlowLayout that in which direction the uicollectionview is scrolling? (Swift)
我知道如何使用滚动视图委托方法检查方向。但不幸的是,我有一种情况,我必须在 UICollectionViewFlowLayout 中做同样的事情,但我无法使用滚动视图委托方法进行访问。
我尝试了检查(在自定义布局内)集合视图在哪个方向滚动的方法,但没有运气。在这种情况下请帮助我。
更多详情:
我想检查自定义布局中的条件。如果滚动方向是垂直的。我想知道它是向上还是向下。
使用您的 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
}
没有任何协议或没有 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
}
}
该功能在某些情况下可能 return 为零,因此请务必在需要时进行检查
这样使用
if getScrollDirection() == .left
我知道如何使用滚动视图委托方法检查方向。但不幸的是,我有一种情况,我必须在 UICollectionViewFlowLayout 中做同样的事情,但我无法使用滚动视图委托方法进行访问。 我尝试了检查(在自定义布局内)集合视图在哪个方向滚动的方法,但没有运气。在这种情况下请帮助我。
更多详情: 我想检查自定义布局中的条件。如果滚动方向是垂直的。我想知道它是向上还是向下。
使用您的 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
}
没有任何协议或没有 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
}
}
该功能在某些情况下可能 return 为零,因此请务必在需要时进行检查
这样使用
if getScrollDirection() == .left