gpt4 book ai didi

ios - UITableView 限制滚动部分

转载 作者:行者123 更新时间:2023-11-28 13:19:33 26 4
gpt4 key购买 nike

我正在尝试找到一种好方法来限制用户在我的 UITableView 中的某些部分滚动。 For instance, when section 3 is selected (by my logic) I would like to have it locked at the top of the screen so you cannot scroll away from it until it is unlocked, but the important part is I still want to keep the responsive UIScrollView 的感觉(即弹跳交互)。

我尝试自己重新创建逻辑,如下所示(效果很好,但没有我正在寻找的那种弹跳感):

override func scrollViewDidScroll(scrollView: UIScrollView) {
let sectionRect = tableView.rectForSection(activeHeaderSection)
let topY = sectionRect.origin.y
let bottomY = topY + sectionRect.height
let frameSize = self.tableView.frame.size.height - self.navigationController!.navigationBar.frame.size.height
let translation = scrollView.panGestureRecognizer.translationInView(scrollView)

if (scrollView.contentOffset.y + frameSize > bottomY) && (frameSize < sectionRect.size.height) && (translation.y < 0) {
UIView.animateLinear(0.7, initialSpringVelocity: 0, animations: { self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.checkTuple[self.activeHeaderSection].pickChecks.count-1, inSection: self.activeHeaderSection), atScrollPosition: .Bottom, animated: false) }, completion: nil)
} else if (frameSize > sectionRect.size.height) || (scrollView.contentOffset.y < topY) {
UIView.animateLinear(0.7, initialSpringVelocity: 0, animations: { self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: self.activeHeaderSection), atScrollPosition: .Top, animated: false) }, completion: nil)
}
}

所以我在这里做了一些搜索,一些问题指向限制 scrollView 的 contentView 高度和位置(即像这样的东西):

override func scrollViewDidScroll(scrollView: UIScrollView) {
var activeHeaderSection = 4
let sectionRect = tableView.rectForSection(activeHeaderSection)

tableView.contentOffset.y = sectionRect.origin.y // Set once when locked
tableView.contentSize.height = sectionRect.size.height
tableView.clipsToBounds = false
}

但这也并没有真正给我我想要的行为,因为它总是粘在我的 UITableView 的顶部(具有正确的高度)但不会向下滚动到正确的部分。如果可能的话,我更愿意使用像第二种方法这样简单的方法,我不确定是否有更简单的方法来完成这样的事情所以我想我会在这里问。

最佳答案

我认为

tableView.contentOffset.y = sectionRect.origin.y
tableView.contentSize.height = sectionRect.size.height

应该是:

tableView.contentInset.top = -sectionRect.origin.y
tableView.contentSize.height = sectionRect.origin.y + sectionRect.size.height

关于ios - UITableView 限制滚动部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27021133/

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