János
János

Reputation: 35112

Why UIView.animate(withDuration has not effect?

I try to perform a layout update, with following lines, but animation is prompt, there is no delay. Any idea why? Using 8.0 Xcode.

UIView.animate(withDuration: 12.0, animations: {

    self.yellowLineLeadingConstraint.isActive = false
    self.yellowLineTrailingConstraint.isActive = false

    self.yellowLineLeadingConstraint = NSLayoutConstraint(item: self.yellowLine, attribute: .leading, relatedBy: .equal, toItem: b, attribute: .leading, multiplier: 1.0, constant: 0)
    self.yellowLineTrailingConstraint = NSLayoutConstraint(item: self.yellowLine, attribute: .trailing, relatedBy: .equal, toItem: b, attribute: .trailing, multiplier: 1.0, constant: 0)

    self.view.addConstraints([self.yellowLineLeadingConstraint, self.yellowLineTrailingConstraint])

    self.yellowLineLeadingConstraint.isActive = true
    self.yellowLineTrailingConstraint.isActive = true
})

Upvotes: 1

Views: 118

Answers (2)

Jon Whitmore
Jon Whitmore

Reputation: 267

You aren't changing any "animatable" attributes. I see you are updating your constraint attribute, but it's not one of these Animatable Properties

Upvotes: -2

Connor Neville
Connor Neville

Reputation: 7361

The proper way to animate layout constraints is to change them beforehand, and call layoutIfNeeded in your animation block (this tells the view to actually update its layout). So your code would look like this:

self.yellowLineLeadingConstraint.isActive = false
self.yellowLineTrailingConstraint.isActive = false

self.yellowLineLeadingConstraint = NSLayoutConstraint(item: self.yellowLine, attribute: .leading, relatedBy: .equal, toItem: b, attribute: .leading, multiplier: 1.0, constant: 0)
self.yellowLineTrailingConstraint = NSLayoutConstraint(item: self.yellowLine, attribute: .trailing, relatedBy: .equal, toItem: b, attribute: .trailing, multiplier: 1.0, constant: 0)

self.view.addConstraints([self.yellowLineLeadingConstraint, self.yellowLineTrailingConstraint])

self.yellowLineLeadingConstraint.isActive = true
self.yellowLineTrailingConstraint.isActive = true

UIView.animate(withDuration: 12.0, animations: {
    self.view.layoutIfNeeded()
}

Upvotes: 4

Related Questions