Skip to content

Instantly share code, notes, and snippets.

@braking
Created August 6, 2013 00:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braking/6161005 to your computer and use it in GitHub Desktop.
Save braking/6161005 to your computer and use it in GitHub Desktop.
The implementation file for an example of animating a UIView using autolayout constraints.
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *viewToAnimate;
@property (nonatomic, assign) BOOL containerIsOpen;
@end
@implementation ViewController
#pragma mark - View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.containerIsOpen = NO;
}
#pragma mark - Interface Actions
- (IBAction)moveButtonTouched
{
if (self.containerIsOpen) {
[self replaceTopConstraintOnView:self.viewToAnimate withConstant:-220.0];
} else {
[self replaceTopConstraintOnView:self.viewToAnimate withConstant:0.0];
}
[self animateConstraints];
self.containerIsOpen = !self.containerIsOpen;
}
#pragma mark - Helper Methods
- (void)replaceTopConstraintOnView:(UIView *)view withConstant:(float)constant
{
[self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if ((constraint.firstItem == view) && (constraint.firstAttribute == NSLayoutAttributeTop)) {
constraint.constant = constant;
}
}];
}
- (void)animateConstraints
{
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
}
@end
@hhanesand
Copy link

I know this is quite old, but for anyone looking at this, you can just connect an outlet for the constraint you want to animate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment