博客> ios - 键盘处理
ios - 键盘处理
2018-07-23 07:41 评论:0 阅读:813 zzczheng
ios - 键盘处理

**_1. > > >

  • (void)keyboardWillChangeFrame:(NSNotification *)note { // 取出键盘最终的frame CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 取出键盘弹出需要花费的时间 double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改transform [UIView animateWithDuration:duration animations:^{ CGFloat ty = [UIScreen mainScreen].bounds.size.height - rect.origin.y; self.view.transform = CGAffineTransformMakeTranslation(0, - ty); }]; }
      • 或者用以下方法

// .监听键盘 // 键盘的frame(位置)即将改变, 就会发出UIKeyboardWillChangeFrameNotification // 键盘即将弹出, 就会发出UIKeyboardWillShowNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; // 键盘即将隐藏, 就会发出UIKeyboardWillHideNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

pragma mark - 键盘处理

/**

  • 键盘即将隐藏 */

    • (void)keyboardWillHide:(NSNotification *)note { // 1.键盘弹出需要的时间 CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    // 2.动画 [UIView animateWithDuration:duration animations:^{ self.toolbar.transform = CGAffineTransformIdentity; }]; }

/**

  • 键盘即将弹出 */

    • (void)keyboardWillShow:(NSNotification *)note { // 1.键盘弹出需要的时间 CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    // 2.动画 [UIView animateWithDuration:duration animations:^{ // 取出键盘高度 CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat keyboardH = keyboardF.size.height; self.toolbar.transform = CGAffineTransformMakeTranslation(0, - keyboardH); }]; }_**

收藏
0
sina weixin mail 回到顶部