gpt4 book ai didi

ios - 当用户在 tableview 中滚动时动画约束变化

转载 作者:行者123 更新时间:2023-11-28 15:56:55 25 4
gpt4 key购买 nike

我有一个 View 作为另外 2 个 View 的容器(一个 View 占屏幕的 1/4,一个 TableView 占屏幕的 3/4)。我想要的是当用户向上滚动(滑动方向)tableview 时,table view 将平滑地上升(向上到屏幕)并在用户向下滚动(滑动方向)时下降。

到目前为止,我已经设法让 tableview 上升,但它上升得太突然,无法让它下降。这是我的操作方法:viewViewTopConstraint 是tableview top constraint to container top constraint

extension StatsOverlayViewController: UIScrollViewDelegate {
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView == statsTableView {
let scrolledOffset = scrollView.contentOffset.y

while viewViewTopConstraint.constant > 0 && viewViewTopConstraint.constant < 69 {
print("scrolling with offset\(scrollView.contentOffset.y)")
self.viewViewTopConstraint.constant -= scrolledOffset
}
}
}

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if scrollView == statsTableView {
if viewViewTopConstraint.constant < 0 {
viewViewTopConstraint.constant = 0
} else if viewViewTopConstraint.constant > 69 {
viewViewTopConstraint.constant = 69
}
}
}

编辑:

将 View 放回原处的问题是由于 scrollview.bounces = true 和 while 循环的错误使用有效的代码是:

       func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView == statsTableView {
let scrolledOffset = scrollView.contentOffset.y
UIView.animateWithDuration(0.2, animations: {
if scrolledOffset > 0 {
self.viewViewTopConstraint.constant = 0
} else {
self.viewViewTopConstraint.constant = self.viewViewTopConstraintOriginalValues
}
self.view.layoutIfNeeded()
})
}
}

最佳答案

您需要在 UIView.animate block 中调用 self.view.layoutIfNeeded

UIView.animate(withDuration: 1) { 
self.view.layoutIfNeeded
}

在更改约束后立即添加此代码。如果你想对你的动画有更多的控制,使用这个

UIView.animate(withDuration: 1, delay: 0, options: [.curveEaseInOut], animations: {
self.view.layoutIfNeeded
}, completion: nil)

关于ios - 当用户在 tableview 中滚动时动画约束变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41596638/

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