以前写的没排版有点乱
整理:http://www.jianshu.com/p/90c5c3a6f760
#pragma mark - 键盘处理
#pragma mark 监听系统发出的键盘通知
- (void)addKeyboardNote
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
// 1.显示键盘
[center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 2.隐藏键盘
[center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark 显示一个新的键盘就会调用
- (void)keyboardWillShow:(NSNotification *)note
{
// 1.取得当前控件最下面的Y值
CGFloat loginBtnMaxY = CGRectGetMaxY(setBtn.frame) + (HEIGHT - 10 - 205)/2;
// 2.取出键盘的高度
CGFloat keyboardH = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
// 3.控制器view的高度 - 键盘的高度
CGFloat keyboardY = self.view.height - keyboardH;
// 3.比较控件最大Y 跟 键盘Y
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
if (duration <= 0.0) {
duration = 0.25;
}
[UIView animateWithDuration:duration animations:^{
if (loginBtnMaxY > keyboardY) { // 键盘挡住了控件
//整体控制器上移
self.view.transform = CGAffineTransformMakeTranslation(0, keyboardY - loginBtnMaxY - 5);
} else { // 没有挡住控件
self.view.transform = CGAffineTransformIdentity;
}
}];
}
#pragma mark 隐藏键盘就会调用
- (void)keyboardWillHide:(NSNotification *)note
{
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}
- (void)dealloc{
NSLog(@"释放");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}







网友评论