键盘的弹出与消失(通知)

来源:互联网 发布:淘宝特卖女装 编辑:程序博客网 时间:2024/05/17 03:07

**键盘的通知
:**
//UIKeyboardWillShowNotification(键盘即将显示)
//UIKeyboardDidShowNotification(键盘已经显示)
//UIKeyboardWillHideNotification(键盘即将隐藏)
//UIKeyboardDidHideNotification(键盘已经隐藏)
//UIKeyboardWillChangeFrameNotification(键盘的位置尺寸即将发生 改变)
//UIKeyboardDidChangeFrameNotification(键盘的位置尺寸已经发生 改变)

1.订阅通知键盘的位置尺寸即将发生 改变

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

2.例 键盘弹出 弹回

- (void)keyboardWillChangeFrame:(NSNotification *)noti{    CGFloat duration = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];    CGRect frame = [noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];    CGFloat offsetY = frame.origin.y - self.view.frame.size.height;    [UIView animateWithDuration:duration animations:^{        self.view.transform = CGAffineTransformMakeTranslation(0, offsetY);    }];}
  1. 最后不要忘了退订通知
- (void)dealloc {    [[NSNotificationCenter defaultCenter] removeObserver:self];}
0 0
原创粉丝点击