【问题标题】:How to undo view Y change when scroll up?向上滚动时如何撤消视图 Y 更改?
【发布时间】:2018-09-01 20:56:39
【问题描述】:

我在视图控制器的下半部分有一个文本视图,我正在为其顶部约束设置动画以在向上/向下滑动时更改 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()
        }
    }

更新:

我在尝试运行更新后的代码时遇到错误。错误是“在展开可选值时意外发现 nil”,我的调试控制台显示 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 中设置约束的地方。我有一个高度

【问题讨论】:

  • 只要加回 220 并在您想要返回时调用 layoutIfNeeded
  • 如何将其链接到相反方向的手势?

标签: ios swift


【解决方案1】:

您可以使用UIPanGestureRecognizer速度检查平移方向。

@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()
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-12-21
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多