5

Using the anchor style NSLayoutConstraints (as in this answer: Swift | Adding constraints programmatically) how can I animate the subviews?

For those who are lazy here is the code:

override func viewDidLoad() {
    super.viewDidLoad()

    let newView = UIView()
    newView.backgroundColor = UIColor.redColor()
    newView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(newView)

    let horizontalConstraint = newView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor)
    let vertivalConstraint = newView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor)
    let widthConstraint = newView.widthAnchor.constraintEqualToAnchor(nil, constant: 100)
    let heightConstraint = newView.heightAnchor.constraintEqualToAnchor(nil, constant: 100)
    NSLayoutConstraint.activateConstraints([horizontalConstraint, vertivalConstraint, widthConstraint, heightConstraint])
}

Specifically, if I want to slide newView right how would I do it? Thanks.

Community
  • 1
  • 1
Liumx31
  • 1,190
  • 1
  • 16
  • 33

1 Answers1

7

You'd set your constraint, and then within an animation block, update with layoutIfNeeded:

self.horizontalConstraint.constant += 10 // move right 10 px.
UIView.animateWithDuration(0.5) {
    self.view.layoutIfNeeded()
}
Chris Slowik
  • 2,859
  • 1
  • 14
  • 27
  • 1
    So I have to assign the constraints to global variables first? my concern is `NSLayoutConstraint.activateConstraints` -- if I change an anchor constraint, do I not need to call `NSLayoutConstraint.activateConstraints` again to activate it? Also what if I want to change the anchor to another UIView? – Liumx31 Sep 26 '15 at 22:15
  • No.. horizontalConstraint is the constraint you set up. it has a property called `constant` that if you add to it will move your view right. The way you have it set up, the constant defaults to 0. Positive numbers are right of center and negative are left of center. Not sure where you got global variables from. – Chris Slowik Sep 26 '15 at 22:54
  • I'm using `centerXAnchor` and `centerYAnchor`, read the code above. What I'm trying to do is to change the anchors. – Liumx31 Sep 26 '15 at 23:06
  • You asked how to animate the views. I showed you how to modify the constraint and animate it - the apple-recommended method of animating views with constraints. First, you update the constraint, then you call `layoutIfNeeded` on the parent view within an animation block. If you were trying to do something else, please update your question. In your code example, you have a horiz constraint on `newView`. If you add to it, you'll push `newView` right. Animate that with an animation block like i showed in my answer. – Chris Slowik Sep 26 '15 at 23:12
  • 1
    You can't change the anchors directly. the anchor is a read-only property that just aids in layout. Thats why you update the `constant` property of the constraint. – Chris Slowik Sep 26 '15 at 23:13
  • @ChrisSlowik what if I want to change horizontal constraint to `view.leadingAnchor` instead, how to do it? – Joe Huang Dec 21 '15 at 09:04
  • Same way you set up the constraint to any other anchor point: `let horizontalConstraint = newView.centerXAnchor.constraintEqualToAnchor(view.leadingAnchor)` – Chris Slowik Dec 29 '15 at 17:15