通过更改约束常量来动画视图

Animate view by changing constraint constant

我有一个 datePicker,我想通过将其顶部约束更改为超级视图顶部来从底部设置动画。

我设置了一个 IBOutlet,在 viewDidLoad 上我可以更改约束常量。

  override func viewDidLoad() {
    super.viewDidLoad()
    self.datePickerTopConstraint.constant = self.view.frame.size.height // I can set this to whatever and it will persist
  }

然而,通过 IBAction,我尝试将常量设置为另一个值,但该值不会持续存在。

@IBAction func showDatePicker() {
    UIView.animateWithDuration(0.25, animations: {
      () -> Void in
      self.datePickerTopConstraint.constant = self.view.frame.size.height - self.datePicker.frame.size.height // Doesn't persist
      self.view.layoutIfNeeded()
    })
  }

看来我可以扭转这一点,让 datePicker 出现在视图中(在 viewDidLoad 中)并将其动画化,但不能让 datePicker 出现在视图外(如上例所示)并在看法。我错过了什么?

编辑

通过将顶部约束常量设置为超级视图的高度,我(出于某种原因我不明白)也将日期选择器的高度设置为 0,这反过来使得 showDatePicker 中的减法毫无意义。

试试这个:

func showDatePicker() {
    self.view.layoutIfNeeded()
    UIView.animateWithDuration(0.25, animations: {
      () -> Void in
      self.datePickerTopConstraint.constant = self.view.frame.size.height - self.datePicker.frame.size.height // Doesn't persist
      self.view.layoutIfNeeded()
    })
  }

您需要在动画块之前和块中调用layoutIfNeeded。现在它正在正确计算视图。就像我说的,在 viewDidLoad 中设置任何约束也是没有意义的,如果你打算在任何地方做,在 viewWillAppear 中做。该视图尚未在 viewDidLoad 中完成设置,因此没有可用于正确设置的约束。在动画块之前调用 layoutIfNeeded 修复了这个错误,无论如何你都需要它,所以它也可以在未来正确计算。

重组代码,以便在按钮的方法中首先计算常量的新值,然后调用动画。将高度计算拉入它自己的函数。我认为 self.datePicker.frame.size.height 不存在并且结果为 0,但我会使用调试器检查它。

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    datePickerTopConstraint.constant = constantForDatePickerViewHeightConstraint()
    view.setNeedsLayout()

}


@IBAction func showDatePicker(button: UIButton) {

    // Check constraint constant
    if datePickerTopConstraint.constant == self.view.frame.size.height {

        // Date picker is NOT visible
        datePickerTopConstraint.constant = constantForDatePickerViewHeightConstraint()

    } else {
        // Date picker is visible
        datePickerTopConstraint.constant = self.view.frame.size.height
    }


    UIView.animateWithDuration(0.25,
        animations: {() -> Void in
        self.view.layoutIfNeeded()
    })
}

private func constantForDateOPickerViewHeightConstraint() -> CGFloat {

    var value : CGFloat = 200.0

    // Workout value you want to as the constant for the constraint.

    return value
}