I have two UIViews
I would like to animate on screen and off. Both should start positioned off screen. The top animates downward, bottom upward. Without constraints it works as expected. But with, the UIViews
are visible at app launch (with a slight animation that is visible). They do animate off screen though.
For the top UIView
, I have a Leading Space, Trailing Space, Top Space, and Height.
For the bottom UIView
, I have a Leading Space, Trailing Space, Bottom Space, and Height.
How should I adjust my code and/or constraints to get the desired animation?
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self animateViewsIn];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.topView.frame = CGRectMake(16.0, -self.topView.frame.size.height, self.topView.frame.size.width, self.topView.frame.size.height);
self.bottomView.frame = CGRectMake(16.0, self.view.bounds.size.height, self.bottomView.frame.size.width, self.bottomView.frame.size.height);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)animateViewsIn {
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.topView.frame = CGRectMake(16.0,
self.topView.frame.size.height/8.0, self.topView.frame.size.width,
self.topView.frame.size.height);
self.bottomView.frame = CGRectMake(16.0, (self.view.bounds.size.height-self.bottomView.frame.size.height)-20.0, self.bottomView.frame.size.width, self.bottomView.frame.size.height);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}
- (IBAction)animateViewsOut:(id)sender {
CGRect basketTopFrame = self.topView.frame;
basketTopFrame.origin.y = -basketTopFrame.size.height;
CGRect basketBottomFrame = self.bottomView.frame;
basketBottomFrame.origin.y = self.view.bounds.size.height;
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
[self.view layoutIfNeeded];
self.topView.frame = basketTopFrame;
self.bottomView.frame = basketBottomFrame;
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}