ios - 向上滚动时如何撤消 View Y 更改?

标签 ios swift

我在我的 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<= 但删除了它以查看是否可能是它,但仍然出现错误。

enter image description here

最佳答案

您可以使用 UIPanGestureRecognizervelocity 检查平移方向。

@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/

相关文章:

objective-c - UIButton不显示文字设置宽度[UIButton setTitle]

ios/iphone sdk 表单管理最佳实践

iphone - ios didselectrowatindexpath 在编辑模式下不调用

ios - NSURLConnection 授权 header 不起作用

ios - 使用 Computed 变量和 Snapkit 时 : No common superview between views

swift - 获取错误域=NSURLErrorDomain代码=-1004 "Could not connect to the server."UserInfo={NSUnderlyingError=0x7fcb0e1de350

ios - 等待异步请求结果

ios - RightBarButtonItem 未显示在正确的位置

ios - UIAlertController 作为 subview Controller

swift - 将 Firebase Firestore 时间戳转换为日期(Swift)?