gpt4 book ai didi

ios - Swift:在滚动时更改 TableView 高度

转载 作者:搜寻专家 更新时间:2023-11-01 06:30:30 24 4
gpt4 key购买 nike

我有一个 VC,如图所示 enter image description here

它在顶部有一个 UICollectionView,在底部有一个 UITableView。CollectionView 有 1:3 的屏幕,TableView 有 2:3 的屏幕(使用 equalHeight 约束设置)。

tableView 滚动 时,我想更改UICollectionView 的高度

tableView 向上滚动时,我想将 equalHeights 约束的乘数分别更改为 collectionView 和 tableView 的 1:5 和 4:5。这将确保 tableView 的高度增加collectionView 减少

tableView向下滚动时,equalHeights 约束的乘数应重置为默认值

我试过向 tableView 添加滑动和平移手势,但它们无法识别。

如何实现这个功能?

P.S:很想通过使用平移手势来实现此功能,以便向上和向下拖动逐渐改变高度。

这是 View 层次结构 enter image description here

编辑/更新

这是我正在使用的代码。

class MyConstant {
var height:CGFloat = 10
}

let myConstant = MyConstant()

主屏幕VC

override func viewWillAppear(_ animated: Bool) {
myConstant.height = self.view.frame.size.height
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (self.lastContentOffset < scrollView.contentOffset.y) {
NotificationCenter.default.post(name: Notifications.decreaseHeightNotification.name, object: nil)
self.topViewConstraint.constant = -self.view.frame.height / 6
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})
} else if (self.lastContentOffset > scrollView.contentOffset.y) {
NotificationCenter.default.post(name: Notifications.increaseHeightNotification.name, object: nil)
self.topViewConstraint.constant = 0
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})
}
self.lastContentOffset = scrollView.contentOffset.y
}

细胞. swift

override func awakeFromNib() {
super.awakeFromNib()

NotificationCenter.default.addObserver(self, selector: #selector(decreaseHeight), name: Notification.Name("decreaseHeightNotification"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(increaseHeight), name: Notification.Name("increaseHeightNotification"), object: nil)

self.contentView.translatesAutoresizingMaskIntoConstraints = false
heightConstraint.constant = (myConstant.height / 3) - 10
widthConstraint.constant = heightConstraint.constant * 1.5
}

@objc func decreaseHeight() {
heightConstraint.constant = (myConstant.height / 6) - 10
widthConstraint.constant = (heightConstraint.constant * 1.5)
self.layoutIfNeeded()
}

@objc func increaseHeight() {
heightConstraint.constant = (myConstant.height / 3) - 10
widthConstraint.constant = (heightConstraint.constant * 1.5)
self.layoutIfNeeded()
}

现在,当我同时滚动两者时,屏幕会卡住。还有调整 collectionViewCell 大小的更好方法吗?

最佳答案

我还没有测试过,但你可以这样做。在此 View 中使用自动布局。它会更好地工作。

将主视图的tableview约束设置为Top, Left, Bottom, Right => 0, 0, 0, 0 并将collectionView放在tableview下,约束为Top,Left,Right, height => 0, 0, 0, x 与主视图。

Note: Tableview is on top of the collectionView.

连接 CollectionView 的高度约束导出并定义 defaultOffset 变量

@IBOutlet weak var defaultHeightConstraint: NSLayoutConstraint
var defaultOffSet: CGPoint?

在viewDidLoad中,

override func viewDidLoad() {
super.viewDidLoad()
tableView.contentInset = UIEdgeInsetsMake(collectionView.size.height, 0, 0, 0)
}

在viewDidAppear中,写

override func viewDidAppear(_ animated: Bool) {
defaultOffSet = tableView.contentOffset
}

在ViewDidScroll中

func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = tableView.contentOffset

if let startOffset = self.defaultOffSet {
if offset.y < startOffset.y {
// Scrolling down
// check if your collection view height is less than normal height, do your logic.


let deltaY = fabs((startOffset.y - offset.y))
defaultHeightConstraint.constant = defaultHeightConstraint.constant - deltaY

} else {
// Scrolling up
let deltaY = fabs((startOffset.y - offset.y))
defaultHeightConstraint.constant = defaultHeightConstraint.constant + deltaY
}

self.view.layoutIfNeeded()
}
}

希望对您有所帮助。

关于ios - Swift:在滚动时更改 TableView 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48096038/

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