Ary*_*rma 2 uitableview uigesturerecognizer ios swift
它的顶部有一个UICollectionView ,底部有一个UITableView 。CollectionView 的屏幕比例为 1:3,TableView 的屏幕比例为 2:3(使用 equalHeight 约束设置)。
我想在tableView 滚动时更改UICollectionView的高度。
当tableView向上滚动时,我想将collectionView和tableView的equalHeights约束的乘数分别更改为1:5和4:5。这将确保tableView的高度增加而collectionView的高度减少
当tableView向下滚动时,equalHeights约束的倍数应该重置为默认值。
我尝试向 tableView 添加滑动和平移手势,但它们无法识别。
如何实现这个功能呢?
PS:希望通过使用平移手势来实现此功能,以便上下拖动逐渐改变高度。
编辑/更新
这是我正在使用的代码。
class MyConstant {
var height:CGFloat = 10
}
let myConstant = MyConstant()
Run Code Online (Sandbox Code Playgroud)
主屏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
}
Run Code Online (Sandbox Code Playgroud)
细胞.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()
}
Run Code Online (Sandbox Code Playgroud)
现在,当我同时滚动两者时,屏幕会冻结。还有调整 collectionViewCell 大小的更好方法吗?
我还没有测试过,但你可以这样做。在此视图中使用自动布局。这样做会更好。
将 tableview 约束设置为 Top, Left, Bottom, Right => 0, 0, 0, 0 与主视图,并将 collectionView 放在 tableview 下,约束为 Top,Left,Right, height => 0, 0, 0, x与主视图。
注意:Tableview 位于 collectionView 之上。
连接 CollectionView 的高度约束出口并定义 defaultOffset 变量
@IBOutlet weak var defaultHeightConstraint: NSLayoutConstraint
var defaultOffSet: CGPoint?
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad中,
override func viewDidLoad() {
super.viewDidLoad()
tableView.contentInset = UIEdgeInsetsMake(collectionView.size.height, 0, 0, 0)
}
Run Code Online (Sandbox Code Playgroud)
在viewDidAppear中写入
override func viewDidAppear(_ animated: Bool) {
defaultOffSet = tableView.contentOffset
}
Run Code Online (Sandbox Code Playgroud)
在 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()
}
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
11724 次 |
| 最近记录: |