1

I want to animate a constraint changing for a custom view.

I've tried ways similar to this:

if widthConstraint.isActive {
     widthConstraint.isActive = false
     widthConstraintA.isActive = true
} else {
     widthConstraintA.isActive = false
     widthConstraint.isActive = true
}
     UIView.animate(withDuration: 1) { 
        imageView.layoutIfNeeded()
}

the same for the multiplier (recreating a new constraint like here Can i change multiplier property for NSLayoutConstraint? ). But all states change instantly.

Is there any method that helps to create smooth interpolated animation between two states?

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • Why change `.isActive` for *two* constraints? Why not simply change `.multiplier` for *one* constraint that is always active? –  Jan 25 '18 at 22:52
  • This is not possible to change a multiplier. This value has only a getter. When I recreate a new constraint I can animate the view. It changes instantly. – Vyacheslav Jan 25 '18 at 23:01

1 Answers1

3

Yes there is suppose that a view1 in storyboard has a width constraint to the main view (self.view in code) like this

  view1Width = view * 0.7 + constant 

instead of creating 2 constraints and switching between them by changing Active property ORRR [by deactivating current constraint with old multiplier and creating a new one with a new multiplier] leave it 1 constraint and play with it's constant

suppose i want to change view1 multiplier to be 0.9 instead of 0.7

I may do this

  self.view1Width.constant += self.view.frame.size.width * 0.2

same you do for subtraction

with this idea you haven't need to be stuck in how to animate active on/off but you still use

  self.view.layoutIfNeeded()
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87