【问题标题】:i am trying to animate my unbutton with a constraint and the animation is not working我正在尝试使用约束为我的取消按钮设置动画,但动画不起作用
【发布时间】:2017-04-27 02:03:28
【问题描述】:

谁能告诉我为什么这不是动画?我觉得代码都很完美,但动画功能不起作用。按钮会立即从一边跳到另一边,但就像我说的那样它没有动画......

func moveResetButton( constraint : NSLayoutConstraint, isOffScreen : Bool)  {

    if isOffScreen {
        //animate the button onto the screen
        UIView.animate(withDuration: 3.0, delay: 0.0, options: .curveLinear, animations: {

            constraint.constant += 120

        }, completion: nil)

    } else {
        //animate the button off of the screen
        UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveLinear, animations: {

            constraint.constant -= 120

        }, completion: nil)
    }
}

【问题讨论】:

  • 我在我的回答中添加了评论。基本上,您正在更改动画内部的约束。那是行不通的。您需要 (1) 更改约束,然后然后 (2) 为更改设置动画。
  • 嘿。是的,我实际上已经弄清楚并添加了它。我在关于 SO 的前一个问题上找到了它,它在 Objective-c 中。我不知道我是如何理解它的,但它确实有效。大声笑感谢您的努力。我希望将来能听到更多来自你的消息。 :)

标签: swift animation uibutton constraints


【解决方案1】:

我想通了。只需在约束更改下的代码中添加self.view.layoutIfNeeded()即可。工作顺利。

【讨论】:

  • 很高兴你知道了。听起来这是一个理解触发的视图(和视图控制器)事件序列的问题。
猜你喜欢
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 2015-09-13
  • 2021-09-15
  • 2018-02-10
相关资源
最近更新 更多