[OC]注册登录界面键盘监听通知与动画结合使用
原文链接:https://www.cnblogs.com/iosliu/p/4425024.html
- (void)viewDidLoad {
[super viewDidLoad];
//键盘监听通知
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo];
//kbSize即為鍵盤尺寸 (有width, height)
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//得到鍵盤的高度
CGFloat keyboardhight = kbSize.height;
//将UISCROLLVIEW上移动 hashKeyBoard=YES;
//设置动画的名字
[UIView beginAnimations:@"AnimationOpen" context:nil];
//设置动画的间隔时间 [UIView setAnimationDuration:0.20];
//??使用当前正在运行的状态开始下一段动画
[UIView setAnimationBeginsFromCurrentState: YES];
//设置视图移动的位移
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - keyboardhight, self.view.frame.size.width, self.view.frame.size.height);
//设置动画结束
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification{
NSDictionary* info = [aNotification userInfo];
//kbSize即為鍵盤尺寸 (有width, height)
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//得到键盘的高度
CGFloat keyboardhight = kbSize.height;
[UIView beginAnimations:@"AnimationOpen" context:nil];
//设置动画的间隔时间
[UIView setAnimationDuration:0.20];
//使用当前正在运行的状态开始下一段动画
[UIView setAnimationBeginsFromCurrentState: YES];
//设置视图移动的位移
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardhight, self.view.frame.size.width, self.view.frame.size.height);
//设置动画结束
[UIView commitAnimations];
}