Vyacheslav
Vyacheslav

Reputation: 27221

Animate NSLayoutConstraints multiplier and/or isActive state

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?

Upvotes: 2

Views: 1102

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100541

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

Upvotes: 3

Related Questions