#import "LoginViewController.h"
#import "ClssNameViewController.h"
@interface LoginViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UIButton *buttonLogin;
@property (strong, nonatomic) IBOutlet UITextField *textName;
@property (strong, nonatomic) IBOutlet UITextField *textPwd;
@property (strong, nonatomic) IBOutlet UITextField *textSelect;
@end
@implementation LoginViewController
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShowOrHide:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShowOrHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Keyboard methods
/*
view将要上升的高度 = 键盘高度-(屏幕高度 - 文本框在view上的y坐标);
*/
-(void)keyboardDidShowOrHide:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardEndFrame;//键盘的frame
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect newFrame = self.view.frame;//view的frame
if (self.textSelect == nil) {
newFrame.origin.y = 0.0;
}
else
{
newFrame.origin.y = keyboardEndFrame.origin.y - newFrame.size.height;
}
self.view.frame = newFrame;
[UIView commitAnimations];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
self.textSelect = textField;
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.textPwd.delegate = self;
self.textName.delegate = self;
UIImageView* imageViewTap = (UIImageView* )[self.view viewWithTag:1];
imageViewTap.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapConcelText:)];
[imageViewTap addGestureRecognizer:tap];
}
-(void)tapConcelText:(UITapGestureRecognizer*)tap
{
self.textSelect = nil;
[self.textPwd resignFirstResponder];
[self.textName resignFirstResponder];
}
@end