我有一个 UIView
锚定到 top
、leading
和 trailing
anchor 。它的初始高度为 138
,并且应该收缩到不小于 88
。
我有一个 UITableView
锚定到这个 View 的底部,它也锚定到 leading
、trailing
和底部 super View 。
它不是 TableView 的标题 View ,而是一个单独的 View 。
在滚动表格 View 时,我希望我的 UIView
缩小并重置回其初始大小,具体取决于滚动的方向。
- 向上滚动 = 缩小到不小于
88
- 向下滚动 = 增加到不超过
138
我目前能够通过使用
func scrollViewDidScroll(_ scrollView: UIScrollView) {
onUserScroll?(scrollView)
}
通过绑定(bind)触发下面的代码
class FeedView: UIView {
private(set) var brandedHeader: UIView!
private(set) var brandedHeaderHeight: NSLayoutConstraint!
private(set) var tableView: UITableView!
private lazy var maxHeaderHeight: CGFloat = 138
private lazy var minHeaderHeight: CGFloat = 88
private var previousScrollOffset: CGFloat = 0
...............
}
extension FeedView {
func setTableViewDelegate(_ delegate: TableViewDelegate) {
tableView.delegate = delegate
tableView.dataSource = delegate
}
func onUserScroll(_ scrollView: UIScrollView) {
let scrollDiff = scrollView.contentOffset.y - self.previousScrollOffset
let absoluteTop: CGFloat = 0
let absoluteBottom: CGFloat = scrollView.contentSize.height - scrollView.frame.size.height
let isScrollingDown = scrollDiff > 0 && scrollView.contentOffset.y > absoluteTop
let isScrollingUp = scrollDiff < 0 && scrollView.contentOffset.y < absoluteBottom
guard canAnimateHeader(scrollView) else { return }
// Calculate new header height
var newHeight = self.brandedHeaderHeight.constant
if isScrollingDown {
newHeight = max(self.minHeaderHeight, self.brandedHeaderHeight.constant - abs(scrollDiff))
} else if isScrollingUp {
newHeight = min(self.maxHeaderHeight, self.brandedHeaderHeight.constant + abs(scrollDiff))
}
// Header needs to animate
if newHeight != self.brandedHeaderHeight.constant {
self.brandedHeaderHeight.constant = newHeight
self.setScrollPosition(self.previousScrollOffset)
}
self.previousScrollOffset = scrollView.contentOffset.y
}
private func canAnimateHeader(_ scrollView: UIScrollView) -> Bool {
// Calculate the size of the scrollView when header is collapsed
let scrollViewMaxHeight = scrollView.frame.height + self.brandedHeaderHeight.constant - minHeaderHeight
// Make sure that when header is collapsed, there is still room to scroll
return scrollView.contentSize.height > scrollViewMaxHeight
}
private func setScrollPosition(_ position: CGFloat) {
self.tableView.contentOffset = CGPoint(x: self.tableView.contentOffset.x, y: position)
}
}
不过,我想做的是延迟调整大小事件。
目前它在向上或向下滚动时立即开始,实际上我希望在用户向上滚动 X 量后开始调整大小,然后仅在他们再次通过该点时向下滚动时才调整大小。
我更新了我的代码如下
private var previousScrollOffset: CGFloat = 100 // offset the initial scroll so the header does not shrink immediately on scroll
然后还有
func onUserScroll(_ scrollView: UIScrollView) {
guard scrollView.contentOffset.y < 200 else { return }
..........
}
但这并没有像预期的那样工作。
希望能实现和大标题导航栏滚动效果一样的效果。
最佳答案
用
更新你的onUserScroll
函数
let isScrollingDown = scrollDiff > 0 && scrollView.contentOffset.y > absoluteTop && scrollView.contentOffset.y > 300
let isScrollingUp = scrollDiff < 0 && scrollView.contentOffset.y < absoluteBottom && scrollView.contentOffset.y < 300
我相信你应该会得到想要的效果。
关于swift - 如何在延迟滚动时更改 UIView 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183297/