【发布时间】:2019-06-27 09:22:54
【问题描述】:
我在屏幕右下方有材质浮动操作按钮 (FAB)。 另外,我在 View 中有 CollectionView。我希望完成以下操作。
- 当用户向下滚动时 - FAB 应该不可见。
- 当用户向上滚动时 - FAB 应该可见。
我在 google 中到处搜索。没有一个问题满足我的要求。
【问题讨论】:
标签: ios swift uicollectionview uiscrollview
我在屏幕右下方有材质浮动操作按钮 (FAB)。 另外,我在 View 中有 CollectionView。我希望完成以下操作。
我在 google 中到处搜索。没有一个问题满足我的要求。
【问题讨论】:
标签: ios swift uicollectionview uiscrollview
别忘了设置collectionView.delegate = self。
extension ViewController: UIScrollViewDelegate{
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == collectoinView{
button.isHidden = scrollView.contentOffset.y > 50
}
}
}
50 是 Y 的位置,按钮将从该位置隐藏。您可以根据您的要求调整到任何数字。
另一种方法
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let targetPoint = targetContentOffset as? CGPoint
let currentPoint = scrollView.contentOffset
if (targetPoint?.y ?? 0.0) > currentPoint.y {
print("up")
} else {
print("down")
}
}
使用第二种方法,不需要提供静态值。第二种方法已从objective-c Answer转换为Swift
【讨论】:
您可以为此使用 scrollViewDidScroll。
func scrollViewDidScroll(scrollView: UIScrollView!) {
if (self.lastContentOffset > scrollView.contentOffset.y) {
// show your button
}
else if (self.lastContentOffset < scrollView.contentOffset.y) {
// hide your button
}
// update the new position acquired
self.lastContentOffset = scrollView.contentOffset.y
}
【讨论】:
https://i.stack.imgur.com/OQGGO.png
【讨论】:
这是上面的代码。
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
moveDownButton.isHidden = false
if targetContentOffset.pointee.y < scrollView.contentOffset.y {
//Going up
setView(view: self.moveDownButton, hidden: false)
} else {
//Going Down
setView(view: self.moveDownButton, hidden: true)
}
}
func setView(view: UIView, hidden: Bool) {
view.isHidden = hidden
}
【讨论】: