简体   繁体   English

折叠标题视图 - ScrollView 在到达底部时停止滚动

[英]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.

相关问题 scrollView不滚动到底部滚动视图上的内容视图不是滚动 - scrollView Not Scrolling to bottom And content view on scroll view was not Scrolling 滚动到 ScrollView 底部时如何防止覆盖 ZStack 中嵌入的 ScrollView? - How to prevent covering a ScrollView embedded in ZStack when scrolling to the bottom of the ScrollView? 如何在 UIScrollView 到达底部时停止滚动 - How to stop a UIScrollView from scrolling when it reaches the bottom Scrollview内的UIColectionView垂直滚动 - 在集合视图区域中滚动时禁用滚动视图的滚动 - UIColectionView Vertical Scrolling inside a Scrollview - Disable scrolling of the scrollview when scrolling in the area of the collection view 自定义scrollView滚动时,移动标题视图 - Custom scrollView when Scroll, move header view 以编程方式将滚动视图滚动到其底部 - scrolling a scrollview to its bottom programmatically ScrollView不滚动到视图的尽头 - ScrollView Not Scrolling to The End of The View 在UITableView中垂直滚动时,在ScrollView标题上重置contentOffset - contentOffset is reset on ScrollView header when vertical scrolling in UITableView 当滚动视图和图像视图都处于情节提要中时,图像未滚动 - image is not scrolling when both scrollview and image view are in storyboard 当另一个视图加载和关闭时,UIScrollView停止滚动 - UIScrollView stops scrolling when another view loads and dismisses
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM