Animate NSLayoutConstraint Change

Hi Guys today we are going to make a tinny sample to demonstrate how to animate NSLayoutConstraint change in a very simple and easy way. are you ready …?

Screen Shot 2015-06-04 at 2.13.51 pm

we have no time to think about it so lets start.

1. Create a new single view based project.

2. Use a storyboard or whatever you like to use and place your UI objects in it and hook your IBOutlets with it

3. Make NSLayoutConstraint IBOutlets and this is done almost 50% 🙂

4. Ok now here we come to place the code

– (IBAction)animateLabels:(id)sender {

    if (lbl1Y.constant == 10) {

        lbl1Y.constant = [UIScreen mainScreen].bounds.size.height40;

        lbl2Y.constant = –150;

        lbl3Y.constant = [UIScreen mainScreen].bounds.size.height40;

        [UIView animateWithDuration:5

                         animations:^{

                             [self.view layoutIfNeeded]; // Called on parent view

                         }];

        

    }else{

        lbl1Y.constant = 10;

        lbl2Y.constant = 0;

        lbl3Y.constant = 10;

        [UIView animateWithDuration:5

                         animations:^{

                             [self.view layoutIfNeeded]; // Called on parent view

                         }];

    }

}

give some numbers to your constraints and after that animate UIView and in animation set “layoutIfNeeded”.

Thats all to make objects animating who are using constraints.

You may get the source code here ConstraintAnimate

Happy Coding

Mind Your Code 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *