#pragma mark --- 键盘生气回收动画通知
- (void)viewWillAppear:(BOOL)animated{
[self KBNotification];
}
- (void)KBNotification{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark --- 视图移动
//键盘升起时动画
- (void)keyboardWillShow:(NSNotification*)notif
{
NSDictionary *info = [notif userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat keyDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
__weak SellerOrderDetaiController * selfVC = self;
CGRect fram = selfVC.view.frame;
if (fram.origin.y < 0) {
CGFloat move =(-fram.origin.y) - keyboardRect.size.height;
[self moveView:move forView:selfVC.view duration:keyDuration];
return;
}
[self moveView:-keyboardRect.size.height forView:selfVC.view duration:keyDuration];
}
//键盘关闭时动画
- (void)keyboardWillHide:(NSNotification*)notif
{
NSDictionary *info = [notif userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat keyDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
__weak SellerOrderDetaiController * selfVC = self;
CGRect fram = selfVC.view.frame;
if (fram.origin.y == 0) {
return;
}
[self moveView:keyboardRect.size.height forView:selfVC.view duration:keyDuration];
}
- (void)moveView:(float)move forView:(UIView *)view duration:(CGFloat)duration{
[UIView animateWithDuration:duration animations:^{
CGRect frame = view.frame;
frame.origin.y += move;
view.frame = frame;
}];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
这个东西在页面之间跳转一定要记得键盘回收,否则系统随便发出通知,导致页面移动