gpt4 book ai didi

ios - 如何实现折叠标题?

转载 作者:行者123 更新时间:2023-11-28 05:41:38 26 4
gpt4 key购买 nike

我试图实现一个可折叠的 UITableView header ,但现在我没有取得任何进展。 This是我的 Storyboard。我尝试使用 scrollViewDidScroll(scrollView: UIScrollView) 委托(delegate)方法,但是在容器中嵌入的 TableView 中滚动时位置根本没有改变。 heightConstraint 是我的容器 header View 的高度。 Here是我类(class)的完整源代码。感谢您的帮助!解决这个问题已经有一段时间了。

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.heightConstraint.constant = self.maxHeaderHeight
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
let absoluteTop: CGFloat = 0
let absoluteBottom: CGFloat = scrollView.contentSize.height - scrollView.frame.size.height
let scrollDiff = scrollView.contentOffset.y - self.previousScrollOffset
let isScrollingDown = scrollDiff > 0 && scrollView.contentOffset.y > absoluteTop
let isScrollingUp = scrollDiff < 0 && scrollView.contentOffset.y < absoluteBottom

var newHeight = self.heightConstraint.constant
if isScrollingDown {
newHeight = max(self.minHeaderHeight, self.heightConstraint.constant - abs(scrollDiff))
} else if isScrollingUp {
newHeight = min(self.maxHeaderHeight, self.heightConstraint.constant + abs(scrollDiff))
}

if newHeight != self.heightConstraint.constant {
self.heightConstraint.constant = newHeight
}

self.previousScrollOffset = scrollView.contentOffset.y
}

向上滚动时标题容器应该消失/改变它的位置。

最佳答案

下面是如何使用 tableView scrolling 处理 headerView height,

class VC: UIViewController {
@IBOutlet weak var heightConstraint: NSLayoutConstraint!

var lastContentOffset: CGFloat = 0.0
let maxHeaderHeight: CGFloat = 115.0

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
//Scrolled to bottom
UIView.animate(withDuration: 0.3) {
self.heightConstraint.constant = 0.0
self.view.layoutIfNeeded()
}
}
else if (scrollView.contentOffset.y < self.lastContentOffset || scrollView.contentOffset.y <= 0) && (self.heightConstraint.constant != self.maxHeaderHeight) {
//Scrolling up, scrolled to top
UIView.animate(withDuration: 0.3) {
self.heightConstraint.constant = self.maxHeaderHeight
self.view.layoutIfNeeded()
}
}
else if (scrollView.contentOffset.y > self.lastContentOffset) && self.heightConstraint.constant != 0.0 {
//Scrolling down
UIView.animate(withDuration: 0.3) {
self.heightConstraint.constant = 0.0
self.view.layoutIfNeeded()
}
}
self.lastContentOffset = scrollView.contentOffset.y
}
}

在上面的代码中headerView会,

  1. collapsetableView 向上滚动
  2. tableView 向下滚动
  3. 展开

如果您仍然遇到任何问题,请告诉我。

关于ios - 如何实现折叠标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56557078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com