Created
April 9, 2014 10:25
-
-
Save jsleeuw/10251898 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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