[英]Collapsing header view - ScrollView stops scrolling when it reaches bottom
I have implemented scrollViewDidScroll so that a header view (pagerView) will collapse as the user scrolls the scrollView.我已经实现了 scrollViewDidScroll 以便当用户滚动 scrollView 时标题视图 (pagerView) 将折叠。 It works perfectly if the content of the scrollView is long enough so that it scrolls off the top of the screen.
如果 scrollView 的内容足够长,它可以完美地滚动到屏幕顶部。 But, if there is less content, the scrollView will stick, ie stop scrolling once it reaches its bottom, and not allow scrolling back down.
但是,如果内容较少,scrollView 会粘住,即一旦到达底部就停止滚动,并且不允许向下滚动。 Any help would be fantastic.
任何帮助都会很棒。
let pagerViewMaxHeight = 350
let pagerViewMinHeight = 44 + statusBarHeight
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let y = scrollView.contentOffset.y
let newPagerViewHeight = pagerViewHeight.constant - y
if newPagerViewHeight > pagerViewMaxHeight {
pagerViewHeight.constant = pagerViewMaxHeight
} else if newPagerViewHeight < pagerViewMinHeight {
pagerViewHeight.constant = pagerViewMinHeight
} else {
pagerViewHeight.constant = newPagerViewHeight
scrollView.contentOffset.y = 0
}
}
Ok I figured it out.好吧,我想通了。 The scrollView was of course stopping scrolling because its content was not larger than the view itself.
scrollView 当然停止滚动,因为它的内容不大于视图本身。 Once the scrollview height and header view matched the height of the entire superview - scrolling disabled.
一旦滚动视图高度和标题视图与整个超级视图的高度匹配 - 滚动禁用。 So, I added another else if condition to only set the new header view height if the scrollViewContentOffsetY was less than its absolute bottom.
因此,我添加了另一个 else if 条件,以便仅在 scrollViewContentOffsetY 小于其绝对底部时才设置新的标题视图高度。
let absoluteBottom: CGFloat = scrollView.contentSize.height - scrollView.frame.size.height;
let scrollViewOffsetY = scrollView.contentOffset.y
let newPagerViewHeight = pagerViewHeight.constant - scrollViewOffsetY
if newPagerViewHeight > pagerViewMaxHeight {
pagerViewHeight.constant = pagerViewMaxHeight
} else if newPagerViewHeight < pagerViewMinHeight && scrollViewOffsetY < absoluteBottom {
pagerViewHeight.constant = pagerViewMinHeight
if newPagerViewHeight < topBarHeight {
UIView.animate(withDuration: 0.3) {
self.titleLabel.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
}
self.title = space.title
}
// if scrollview offset is less than its absolute bottom, adjust pagerView height
} else if scrollViewOffsetY < absoluteBottom && Int(scrollViewOffsetY) != Int(absoluteBottom) {
pagerViewHeight.constant = newPagerViewHeight
scrollView.contentOffset.y = 0
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.