【问题标题】:Animating Auto Layout Constraints Not Working动画自动布局约束不起作用
【发布时间】:2017-04-06 23:32:20
【问题描述】:

iOS 10、Swift 3.1

我在一个 UITableViewCell 中有一个名为 graphPopupUIView,我正在尝试对其进行动画处理。基本上,当有人点击图表时,会出现一个弹出窗口,我希望它能够将其 xy 设置为动画。

除了弹出窗口的动画外,一切正常。这是它现在的样子:

我希望弹出窗口从点到点滑动到位。这是我的代码:

class TotalCell: UITableViewCell, ChartViewDelegate{

  @IBOutlet weak var graphPopup: UIView!
  @IBOutlet weak var popupConstraintY: NSLayoutConstraint!
  @IBOutlet weak var popupConstraintX: NSLayoutConstraint!

  func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
    //...
    graphPopup.isHidden = false

    //Set new auto layout constraints
    self.popupConstraintX.constant = highlight.xPx-44
    self.popupConstraintY.constant = highlight.yPx+18

    //Animate (which does nothing)   
    UIView.animate(withDuration: 1.0, animations: {
      self.graphPopup.layoutIfNeeded()
    })
  }
}

知道我可能做错了什么吗?如果我完全删除动画位,它的行为方式相同,但我知道正在调用动画。

有什么想法吗?

【问题讨论】:

  • 两个问题 - 您是否尝试在 self 上调用 layoutIfNeeded(),插入 graphPopup?第二 - 你在哪里隐藏弹出窗口?你试过让它一直可见吗?这会很奇怪,但也许动画是在不可见的时候发生的?

标签: ios autolayout uiviewanimation ios-charts


【解决方案1】:

尝试self.layoutIfNeeded() 而不是self.graphPopup.layoutIfNeeded()

【讨论】:

  • 奇怪的是,我将视图控制器的外观从 present 更改为 push 并且动画正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-04
  • 2019-06-05
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
相关资源
最近更新 更多