当用户向下滚动时,材质浮动操作按钮应该不可见,向上滚动时应该可见 [英] Material floating action button should invisible when user scroll down and visible when scrolls up
问题描述
我在屏幕右下角有材质浮动操作按钮 (FAB).另外,我在 View 中有 CollectionView.我希望完成以下操作.
I have material Floating action button (FAB) at right bottom of screen. Also, I have CollectionView inside View. I want below actions to be done.
- 当用户向下滚动时 - FAB 应该不可见.
- 当用户向上滚动时 - FAB 应该可见.
我在谷歌上到处搜索.没有一个问题满足我的要求.
I've searched everywhere in google.None of questions satisfied my requirements.
推荐答案
不要忘记设置 collectionView.delegate = self.
don't forget to set collectionView.delegate = self.
extension ViewController: UIScrollViewDelegate{
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == collectoinView{
button.isHidden = scrollView.contentOffset.y > 50
}
}
}
50 是按钮将隐藏的 Y 位置.您可以根据您的要求调整到任何数字.
50 is the position of Y from which the button will hide. You can adjust to any number according to your requirement.
另一种方法
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转换为Swift Answer
with the second approach, there is no need to provide static value. the second approach has been converted to Swift from objective-c Answer
这篇关于当用户向下滚动时,材质浮动操作按钮应该不可见,向上滚动时应该可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!