Showing posts with label constraint. Show all posts
Showing posts with label constraint. Show all posts

Friday, 18 March 2016

View Animation

A constraint defines a relationship between two user interface objects that must be satisfied by the constraint-based layout system.

For presenting a view with constraint if little bit different.
We can use frame property for animating a view object.
We can change size and origin by setting new value inside animation block.

Here an example for animating 2 UIView object with constraint.

1) Add one UIView object on XIB file.
set following constraint
  a) Leading constraint(Margin from left)  = 0
  b)Tailing constraint(margin from right) = 0
  c) Top constraint(from super view) = 0
  d) Height constraint = 0

2) Add one more UIView object.(for differentiate set different-2 background color)
 set following constraint on newly added UIView object
  a) Leading constraint(Margin from left)  = 0
  b)Tailing constraint(margin from right) = 0
  c) Top constraint (From first UIView object not from super view)= 0
  d)Bottom constraint = 0

Now, define a IBOutlet property for NSLayoutConstraint
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *topHeightConstraint;

On XIB connect topHeightConstraint with Height Constraint of Top or first UIView object.






Add a button for animating both view.
Here action of button

-(void) animationButtonAction:(UIBarButtonItem *)barButton{
    if (self.topHeightConstraint.constant == 0) {
        NSLog(@"Increase height");
        self.topHeightConstraint.constant = 300;
    }
    else{
        NSLog(@"Decrease height");
        self.topHeightConstraint.constant = 0;
    }
    [UIView animateWithDuration:1 animations:^{
        [self.view layoutIfNeeded];
    }];
}


Now, Its all set for animating UIView objects with NSLayoutConstraint.=