我在我的 View Controller 的下半部分有一个 TextView ,我正在动画它的顶部约束以在您向上/向下滑动时更改 Y 以获得更“全屏”的外观,但我不知道如何撤消它反向所以它回到原来的位置
这是我的代码:
@IBOutlet weak var viewAllTextViews: UIView!
@IBOutlet weak var topOfViewAllTextViews: NSLayoutConstraint!
@IBAction func growViewAllTextViews(_ sender: UIPanGestureRecognizer) {
self.topOfViewAllTextViews.constant = -220
UIView.animate(withDuration: 1.0) {
self.view.layoutIfNeeded()
}
}
更新:
当我尝试运行更新后的代码时出现错误。错误是“Unexpectedly found nil while unwrapping an Optional value”,我的调试控制台显示 topOfViewAllTextViews = (NSLayoutConstraint?)nil。
这是我推荐的代码:
@IBAction func growViewAllTextViews(_ sender: UIPanGestureRecognizer) {
let velocity = sender.velocity(in: myView)
if velocity.y > 0 {
self.topOfViewAllTextViews.constant = 0
UIView.animate(withDuration: 1.0) {
self.view.layoutIfNeeded()
}
} else {
self.topOfViewAllTextViews.constant = -215 // error here
UIView.animate(withDuration: 1.0) {
self.view.layoutIfNeeded()
}
}
}
这是我在 Storyboard 中设置约束的地方。我有一个 height<= 但删除了它以查看是否可能是它,但仍然出现错误。
最佳答案
您可以使用 UIPanGestureRecognizer
的velocity 检查平移方向。
@IBAction func growViewAllTextViews(_ sender: UIPanGestureRecognizer) {
let velocity = gesture.velocity(in: yourView)
if velocity.y > 0 {
self.topOfViewAllTextViews.constant = 220
UIView.animate(withDuration: 1.0) {
self.view.layoutIfNeeded()
}
} else {
self.topOfViewAllTextViews.constant = -220
UIView.animate(withDuration: 1.0) {
self.view.layoutIfNeeded()
}
}
}
关于ios - 向上滚动时如何撤消 View Y 更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52132117/