Created
March 13, 2014 09:49
-
-
Save keicoder/9525280 to your computer and use it in GitHub Desktop.
objective-c : viewController with XIB and keyboard accview delegate
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
//viewController with XIB and keyboard accview delegate | |
//AppDelegate.h | |
@class ViewController; | |
@interface AppDelegate : UIResponder <UIApplicationDelegate> | |
// viewController with XIB | |
// @property (strong, nonatomic) ViewController *viewController; | |
@end | |
//AppDelegate.m | |
#import "ViewController.h" | |
@implementation AppDelegate | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
// viewController with XIB | |
// self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | |
// self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; | |
// self.window.rootViewController = self.viewController; | |
// [self.window makeKeyAndVisible]; | |
return YES; | |
} | |
//UIKeyboardCoView.h | |
@protocol UIKeyboardCoViewDelegate; | |
#define UIKeyboardCoViewWillRotateNotification @"UIKeyboardCoViewWillRotateNotification" | |
#define UIKeyboardCoViewDidRotateNotification @"UIKeyboardCoViewDidRotateNotification" | |
@interface UIKeyboardCoView : UIView | |
@property (nonatomic,assign) IBOutlet id<UIKeyboardCoViewDelegate> delegate; | |
@end | |
//Protocol to implement for a UIKeyboardCoView delegate | |
@protocol UIKeyboardCoViewDelegate <NSObject> | |
@optional | |
- (void) keyboardCoViewWillAppear:(UIKeyboardCoView*)keyboardCoView; | |
- (void) keyboardCoViewDidAppear:(UIKeyboardCoView*)keyboardCoView; | |
- (void) keyboardCoViewWillDisappear:(UIKeyboardCoView*)keyboardCoView; | |
- (void) keyboardCoViewDidDisappear:(UIKeyboardCoView*)keyboardCoView; | |
@end | |
//UIKeyboardCoView.m | |
@interface UIKeyboardCoView () | |
@property (nonatomic,assign) BOOL isRotating; //Is set to true when a Will Rotate notification is posted, and to false when a Did Rotate notification is posted | |
- (void)keyboardCoViewCommonInit; | |
- (void)keyboardWillAppear:(NSNotification*)notification; | |
- (void)keyboardWillDisappear:(NSNotification*)notification; | |
- (void)viewControllerWillRotate:(NSNotification*)notification; | |
- (void)viewControllerDidRotate:(NSNotification*)notification; | |
//below method fix a CGRect from the keyboard show and hide notifications and transforms it into a relative to this view's superview CGRect | |
//returns The fixed and this view's superview relative CGRect | |
- (CGRect)fixKeyboardRect:(CGRect)originalRect; | |
@end | |
@implementation UIKeyboardCoView | |
#pragma mark - Init Methods | |
- (id) init | |
{ | |
self = [super init]; | |
if (self){ | |
[self keyboardCoViewCommonInit]; | |
} | |
return self; | |
} | |
- (id) initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self){ | |
[self keyboardCoViewCommonInit]; | |
} | |
return self; | |
} | |
- (id) initWithCoder:(NSCoder *)aDecoder | |
{ | |
self = [super initWithCoder:aDecoder]; | |
if (self){ | |
[self keyboardCoViewCommonInit]; | |
} | |
return self; | |
} | |
- (void) keyboardCoViewCommonInit | |
{ | |
//It's not rotating | |
self.isRotating = NO; | |
//Register for notifications | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewControllerWillRotate:) name:UIKeyboardCoViewWillRotateNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewControllerDidRotate:) name:UIKeyboardCoViewDidRotateNotification object:nil]; | |
} | |
#pragma mark - Dealloc method | |
- (void) dealloc | |
{ | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
[super dealloc]; | |
} | |
#pragma mark - Keyboard notification methods | |
- (void) keyboardWillAppear:(NSNotification*)notification{ | |
//Get begin, ending rect and animation duration | |
CGRect beginRect = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; | |
CGRect endRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; | |
CGFloat animDuration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; | |
//Transform rects to local coordinates | |
beginRect = [self fixKeyboardRect:beginRect]; | |
endRect = [self fixKeyboardRect:endRect]; | |
//Get this view begin and end rect | |
CGRect selfBeginRect = CGRectMake(beginRect.origin.x, | |
beginRect.origin.y - self.frame.size.height, | |
beginRect.size.width, | |
self.frame.size.height); | |
CGRect selfEndingRect = CGRectMake(endRect.origin.x, | |
endRect.origin.y - self.frame.size.height, | |
endRect.size.width, | |
self.frame.size.height); | |
//Set view position and hidden | |
self.frame = selfBeginRect; | |
self.alpha = 0.0f; | |
[self setHidden:NO]; | |
//If it's rotating, begin animation from current state | |
UIViewAnimationOptions options = UIViewAnimationOptionAllowAnimatedContent; | |
if (self.isRotating){ | |
options |= UIViewAnimationOptionBeginFromCurrentState; | |
} | |
//Start the animation | |
if ([self.delegate respondsToSelector:@selector(keyboardCoViewWillAppear:)]) | |
[self.delegate keyboardCoViewWillAppear:self]; | |
[UIView animateWithDuration:animDuration delay:0.0f | |
options:options | |
animations:^(void){ | |
self.frame = selfEndingRect; | |
self.alpha = 1.0f; | |
} | |
completion:^(BOOL finished){ | |
self.frame = selfEndingRect; | |
self.alpha = 1.0f; | |
if ([self.delegate respondsToSelector:@selector(keyboardCoViewDidAppear:)]) | |
[self.delegate keyboardCoViewDidAppear:self]; | |
}]; | |
} | |
- (void) keyboardWillDisappear:(NSNotification*)notification{ | |
//Start animation ONLY if the view will not rotate | |
if (!self.isRotating){ | |
//Get begin, ending rect and animation duration | |
CGRect beginRect = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; | |
CGRect endRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; | |
CGFloat animDuration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; | |
//Transform rects to local coordinates | |
beginRect = [self fixKeyboardRect:beginRect]; | |
endRect = [self fixKeyboardRect:endRect]; | |
//Get this view begin and end rect | |
CGRect selfBeginRect = CGRectMake(beginRect.origin.x, | |
beginRect.origin.y - self.frame.size.height, | |
beginRect.size.width, | |
self.frame.size.height); | |
CGRect selfEndingRect = CGRectMake(endRect.origin.x, | |
endRect.origin.y - self.frame.size.height, | |
endRect.size.width, | |
self.frame.size.height); | |
//Set view position and hidden | |
self.frame = selfBeginRect; | |
self.alpha = 1.0f; | |
//Animation options | |
UIViewAnimationOptions options = UIViewAnimationOptionAllowAnimatedContent; | |
//Animate view | |
if ([self.delegate respondsToSelector:@selector(keyboardCoViewWillDisappear:)]) | |
[self.delegate keyboardCoViewWillDisappear:self]; | |
[UIView animateWithDuration:animDuration delay:0.0f | |
options:options | |
animations:^(void){ | |
self.frame = selfEndingRect; | |
self.alpha = 0.0f; | |
} | |
completion:^(BOOL finished){ | |
self.frame = selfEndingRect; | |
self.alpha = 0.0f; | |
[self setHidden:YES]; | |
if ([self.delegate respondsToSelector:@selector(keyboardCoViewDidDisappear:)]) | |
[self.delegate keyboardCoViewDidDisappear:self]; | |
}]; | |
} | |
} | |
#pragma mark - Custom rotation notification methods | |
- (void) viewControllerWillRotate:(NSNotification*)notification{ | |
//Is rotating | |
self.isRotating = YES; | |
} | |
- (void) viewControllerDidRotate:(NSNotification*)notification{ | |
//Is not rotating | |
self.isRotating = NO; | |
} | |
#pragma mark - Private methods | |
- (CGRect) fixKeyboardRect:(CGRect)originalRect{ | |
//Get the UIWindow by going through the superviews | |
UIView * referenceView = self.superview; | |
while ((referenceView != nil) && ![referenceView isKindOfClass:[UIWindow class]]){ | |
referenceView = referenceView.superview; | |
} | |
//If we finally got a UIWindow | |
CGRect newRect = originalRect; | |
if ([referenceView isKindOfClass:[UIWindow class]]){ | |
//Convert the received rect using the window | |
UIWindow * myWindow = (UIWindow*)referenceView; | |
newRect = [myWindow convertRect:originalRect toView:self.superview]; | |
} | |
//Return the new rect (or the original if we couldn't find the Window -> this should never happen if the view is present) | |
return newRect; | |
} | |
@end | |
//ViewController.h | |
#import "UIKeyboardCoView.h" | |
@interface ViewController : UIViewController <UIKeyboardCoViewDelegate> | |
@property (retain, nonatomic) IBOutlet UITextField *texField; | |
@end | |
//ViewController.m | |
@implementation ViewController | |
@synthesize texField; | |
#pragma mark - View lifecycle | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
} | |
#pragma mark - Rotation control | |
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration | |
{ | |
[[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardCoViewWillRotateNotification object:nil]; | |
} | |
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation | |
{ | |
[[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardCoViewDidRotateNotification object:nil]; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
return YES; | |
} | |
#pragma mark - UI Keyboard Co View Delegate | |
- (void) keyboardCoViewWillAppear:(UIKeyboardCoView*)keyboardCoView | |
{ | |
NSLog(@"Keyboard Co View Will Appear"); | |
} | |
- (void) keyboardCoViewDidAppear:(UIKeyboardCoView*)keyboardCoView | |
{ | |
NSLog(@"Keyboard Co View Did Appear"); | |
} | |
- (void) keyboardCoViewWillDisappear:(UIKeyboardCoView*)keyboardCoView | |
{ | |
NSLog(@"Keyboard Co View Will Disappear"); | |
} | |
- (void) keyboardCoViewDidDisappear:(UIKeyboardCoView*)keyboardCoView | |
{ | |
NSLog(@"Keyboard Co View Did Disappear"); | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment