ios - 当用户向下滚动和向上滚动时,材质浮动操作按钮应该不可见
问题描述
我在屏幕右下方有材质浮动操作按钮 (FAB)。另外,我在 View 中有 CollectionView。我希望完成以下操作。
- 当用户向下滚动时 - FAB 应该不可见。
- 当用户向上滚动时 - FAB 应该可见。
我在谷歌到处搜索。没有一个问题能满足我的要求。
解决方案
不要忘记设置 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
推荐阅读
- regex - 如何在 Google 表格中使用 regexextract 函数来获取引号中包含的文本和数字
- reactjs - 使用 CDN 导入库并在组件中使用其对象
- javascript - 如何从对象的属性中渲染组件?
- amazon-web-services - 在 AWS EKS 上使用 Kubernetes 身份验证方法部署 Hashicorp Vault 时出现证书错误
- android - Firebase 应用程序不会将用户添加到我的实时数据库
- css - 如何在 Firefox 和 Safari 中模拟 SVG `pointer-events: bounding-box`
- django - django:登录表单无效
- mysql - 获取 group by 子句 MySQL 中的额外属性
- javascript - 从多个中获取单个数组
- wordpress - 当我在 word press admin 支持的用户搜索框中搜索单引号时遇到 404 错误