//3.监听键盘通知
[[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 *)note
{
//1.取出键盘的frame
CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//2.取出键盘弹出的时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//3.执行动画
[UIView animateWithDuration:duration animations:^{
self.toolbar.transform = CGAffineTransformMakeTranslation(0, -keyboardF.size.height);
}];
}
/**
* 键盘即将退出的时候调用
*/
- (void)keyboardWillHide:(NSNotification *)note
{
//1.取出键盘弹出的时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//2.执行动画
[UIView animateWithDuration:duration animations:^{
self.toolbar.transform = CGAffineTransformIdentity;
}];
}