Last active
August 29, 2015 13:58
-
-
Save smnh/10326976 to your computer and use it in GitHub Desktop.
Synchronizing rotation animation between the keyboard and the attached view
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
- (void)viewWillAppear:(BOOL)animated { | |
[super viewWillAppear:animated]; | |
[[NSNotificationCenter defaultCenter] | |
addObserver:self selector:@selector(keyboardWillShow:) | |
name:UIKeyboardWillShowNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] | |
addObserver:self selector:@selector(keyboardWillHide:) | |
name:UIKeyboardWillHideNotification object:nil]; | |
} | |
- (void)viewDidDisappear:(BOOL)animated { | |
[super viewDidDisappear:animated]; | |
[[NSNotificationCenter defaultCenter] | |
removeObserver:self name:UIKeyboardWillShowNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] | |
removeObserver:self name:UIKeyboardWillHideNotification object:nil]; | |
} | |
// #1 | |
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { | |
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; | |
self.animatingRotation = YES; | |
} | |
// #3 | |
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { | |
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; | |
} | |
// #5 | |
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { | |
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; | |
self.animatingRotation = NO; | |
} | |
// #4 | |
- (void)keyboardWillShow:(NSNotification*)notification { | |
[self adjustViewForKeyboardNotification:notification]; | |
} | |
// #2 | |
- (void)keyboardWillHide:(NSNotification*)notification { | |
[self adjustViewForKeyboardNotification:notification]; | |
} | |
- (void)adjustViewForKeyboardNotification:(NSNotification *)notification { | |
NSDictionary *notificationInfo = [notification userInfo]; | |
// Get the end frame of the keyboard in screen coordinates. | |
CGRect finalKeyboardFrame = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; | |
// Convert the finalKeyboardFrame to view coordinates to take into account any rotation | |
// factors applied to the window’s contents as a result of interface orientation changes. | |
finalKeyboardFrame = [self.view convertRect:finalKeyboardFrame fromView:self.view.window]; | |
// Calculate new position of the commentBar | |
CGRect commentBarFrame = self.commentBar.frame; | |
commentBarFrame.origin.y = finalKeyboardFrame.origin.y - commentBarFrame.size.height; | |
// Update tableView height. | |
CGRect tableViewFrame = self.tableView.frame; | |
tableViewFrame.size.height = commentBarFrame.origin.y; | |
if (!self.animatingRotation) { | |
// Get the animation curve and duration | |
UIViewAnimationCurve animationCurve = (UIViewAnimationCurve) [[notificationInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]; | |
NSTimeInterval animationDuration = [[notificationInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; | |
// Animate view size synchronously with the appearance of the keyboard. | |
[UIView beginAnimations:nil context:nil]; | |
[UIView setAnimationDuration:animationDuration]; | |
[UIView setAnimationCurve:animationCurve]; | |
[UIView setAnimationBeginsFromCurrentState:YES]; | |
self.commentBar.frame = commentBarFrame; | |
self.tableView.frame = tableViewFrame; | |
[UIView commitAnimations]; | |
} else { | |
self.commentBar.frame = commentBarFrame; | |
self.tableView.frame = tableViewFrame; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment