【发布时间】:2018-01-04 13:08:06
【问题描述】:
顶部有一个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:希望通过使用平移手势来实现此功能,以便向上和向下拖动逐渐改变高度。
编辑/更新
这是我正在使用的代码。
class MyConstant {
var height:CGFloat = 10
}
let myConstant = MyConstant()
MainScreenVC
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
}
Cell.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 的大小?
【问题讨论】:
-
你能把你的代码sn-p贴在这里吗?如果没有您使用的方法,这个问题是不完整的。一旦可用,您就可以期待相关的答案。
-
@nothingwhatsoever 看看
-
为什么要增加内容视图的高度?它应该是前导,尾随,顶部,底部和单元格,因此它会根据单元格自动增加高度。在表格视图上滚动而不是发布通知后,使用集合视图无效,请参阅此 aplus.rs/2015/… 。您在 Indexpath 中的项目高度做什么。
标签: ios swift uitableview uigesturerecognizer