Skip to content

Instantly share code, notes, and snippets.

@jsleeuw
Created April 9, 2014 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsleeuw/10251898 to your computer and use it in GitHub Desktop.
Save jsleeuw/10251898 to your computer and use it in GitHub Desktop.
#import "AutoLayoutStandardViewController.h"
@interface AutoLayoutStandardViewController ()
@property (strong, nonatomic) NSLayoutConstraint *constraintToAnimate;
@property (strong, nonatomic) UILabel *label;
- (void)animateLabel;
@end
@implementation AutoLayoutStandardViewController
- (void)loadView
{
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *mainView = [[UIView alloc] initWithFrame:applicationFrame];
mainView.backgroundColor = [UIColor whiteColor];
self.view = mainView;
self.label = [UILabel new];
self.label.text = @"This is a Label";
self.label.font = [UIFont systemFontOfSize:18];
self.label.backgroundColor = [UIColor lightGrayColor];
self.label.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.label];
self.constraintToAnimate = [NSLayoutConstraint constraintWithItem:self.label
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:0.25
constant:0.0];
[self.view addConstraint:self.constraintToAnimate];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:@selector(animateLabel) withObject:nil afterDelay:2.0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)animateLabel {
[self.view removeConstraint:self.constraintToAnimate];
self.constraintToAnimate = [NSLayoutConstraint constraintWithItem:self.label
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:0.5
constant:0.0];
[self.view addConstraint:self.constraintToAnimate];
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment