简体   繁体   中英

Animating Auto Layout Constraints Not Working

iOS 10, Swift 3.1

I have a UIView called graphPopup inside a UITableViewCell that I'm trying to animate. Basically when someone taps a graph, a popup appears and I'd like it to animate its x and y into place.

Everything is working except the animation of the popup. Here's how it looks now: 在此处输入图片说明

I want the popup to slide from point-to-point into position. Here is my code:

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

Any idea what I might be doing wrong? If I remove the animation bit entirely, it behaves the same way, but I know the animation is being called.

Any ideas?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM