Created
March 26, 2012 18:26
-
-
Save AquaGeek/2208495 to your computer and use it in GitHub Desktop.
Blocks and retain cycles
This file contains hidden or 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]; | |
| // Adjust the table view's content inset when the keyboard appears/disappears | |
| _keyboardWillShowNotification = [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification | |
| object:nil | |
| queue:nil | |
| usingBlock:^(NSNotification *note) | |
| { | |
| // Adjust the table view's content inset to the top of the keyboard | |
| NSDictionary *keyboardInfo = [note userInfo]; | |
| NSUInteger animationCurve = [[keyboardInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue]; | |
| NSNumber *duration = [keyboardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; | |
| NSValue *endFrameVal = [keyboardInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; | |
| CGRect endFrame = CGRectZero; | |
| [endFrameVal getValue:&endFrame]; | |
| [UIView animateWithDuration:[duration doubleValue] | |
| delay:0 | |
| options:(animationCurve << 16) // In UIViewAnimationOptions, curve options start at 0 << 16 | |
| animations:^ | |
| { | |
| UIEdgeInsets insets = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(endFrame), 0.0f); | |
| self.tableView.contentInset = insets; | |
| self.tableView.scrollIndicatorInsets = insets; | |
| } | |
| completion:^(BOOL finished) | |
| { | |
| }]; | |
| }]; | |
| _keyboardWillHideNotification = [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification | |
| object:nil | |
| queue:nil | |
| usingBlock:^(NSNotification *note) | |
| { | |
| // Adjust the table view's content inset back to the bottom of the screen | |
| NSDictionary *keyboardInfo = [note userInfo]; | |
| NSUInteger animationCurve = [[keyboardInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue]; | |
| NSNumber *duration = [keyboardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; | |
| [UIView animateWithDuration:[duration doubleValue] | |
| delay:0 | |
| options:(animationCurve << 16) | |
| animations:^ | |
| { | |
| self.tableView.contentInset = UIEdgeInsetsZero; | |
| self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero; | |
| } | |
| completion:^(BOOL finished) | |
| { | |
| }]; | |
| }]; | |
| } |
This file contains hidden or 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]; | |
| // If we don't use a weak variable, the notification blocks retain self and the view controller is never dealloc'd | |
| __weak MyViewController *weakSelf = self; | |
| _keyboardWillShowNotification = [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification | |
| object:nil | |
| queue:nil | |
| usingBlock:^(NSNotification *note) | |
| { | |
| // Adjust the table view's content inset to the top of the keyboard | |
| NSDictionary *keyboardInfo = [note userInfo]; | |
| NSUInteger animationCurve = [[keyboardInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue]; | |
| NSNumber *duration = [keyboardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; | |
| NSValue *endFrameVal = [keyboardInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; | |
| CGRect endFrame = CGRectZero; | |
| [endFrameVal getValue:&endFrame]; | |
| [UIView animateWithDuration:[duration doubleValue] | |
| delay:0 | |
| options:(animationCurve << 16) // In UIViewAnimationOptions, curve options start at 0 << 16 | |
| animations:^ | |
| { | |
| UIEdgeInsets insets = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(endFrame), 0.0f); | |
| weakSelf.tableView.contentInset = insets; | |
| weakSelf.tableView.scrollIndicatorInsets = insets; | |
| } | |
| completion:^(BOOL finished) | |
| { | |
| }]; | |
| }]; | |
| _keyboardWillHideNotification = [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification | |
| object:nil | |
| queue:nil | |
| usingBlock:^(NSNotification *note) | |
| { | |
| // Adjust the table view's content inset back to the bottom of the screen | |
| NSDictionary *keyboardInfo = [note userInfo]; | |
| NSUInteger animationCurve = [[keyboardInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue]; | |
| NSNumber *duration = [keyboardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; | |
| [UIView animateWithDuration:[duration doubleValue] | |
| delay:0 | |
| options:(animationCurve << 16) | |
| animations:^ | |
| { | |
| weakSelf.tableView.contentInset = UIEdgeInsetsZero; | |
| weakSelf.tableView.scrollIndicatorInsets = UIEdgeInsetsZero; | |
| } | |
| completion:^(BOOL finished) | |
| { | |
| }]; | |
| }]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment