完美处理弹出键盘,界面上移功能

int          m_curKeyboardHeight;   在init函数中  设置为 m_curKeyboardHeight =0;   2. 在init中  注册两个通知

        [[NSNotificationCenter defaultCenteraddObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];

        [[NSNotificationCenter defaultCenteraddObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];

 

      在dealloc中,注销这两个通知:

      

        [[NSNotificationCenter defaultCenterremoveObserver:selfname:UIKeyboardWillShowNotification object:nil];

        [[NSNotificationCenter defaultCenterremoveObserver:selfname:UIKeyboardWillHideNotification object:nil];

 

     3. 通知响应函数 的定义以及实现。

      假设  输入框为  

UITextField     *  m_inputField;     – (void)keyboardWillShow:(NSNotification *)aNotification; – (void)keyboardWillHide:(NSNotification *)aNotification;     – (void)keyboardWillShow:(NSNotification *)aNotification {

    if ([m_inputField isFirstResponder] == NO)

    { return; } CGRect keyboardRect = [[[aNotification userInfoobjectForKey:UIKeyboardFrameEndUserInfoKeyCGRectValue];  

    NSTimeInterval animationDuration = [[[aNotification userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKeydoubleValue];

    CGRect bottomframe = m_bottomImage.frame; bottomframe.origin.y -= keyboardRect.size.width – m_curKeyboardHeight; CGRect fieldframe = m_inputField.frame; fieldframe.origin.y -= keyboardRect.size.width – m_curKeyboardHeight;

    [UIView beginAnimations:@”ResizeForKeyboard” context:nil];

    [UIView setAnimationDuration:animationDuration]; m_bottomImage.frame = bottomframe; m_inputField.frame = fieldframe;  

    [UIView commitAnimations];

m_curKeyboardHeight = keyboardRect.size.width; }   – (void)keyboardWillHide:(NSNotification *)aNotification {

    if ([m_inputField isFirstResponder] == NO)

    { return; }

    NSTimeInterval animationDuration = [[[aNotification userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKeydoubleValue];

CGRect bottomframe = m_bottomImage.frame; bottomframe.origin.y += m_curKeyboardHeight; CGRect fieldframe = m_inputField.frame; fieldframe.origin.y+= m_curKeyboardHeight;  

    [UIView beginAnimations:@”ResizeForKeyboard” context:nil];

    [UIView setAnimationDuration:animationDuration]; m_bottomImage.frame = bottomframe; m_inputField.frame = fieldframe;  

    [UIView commitAnimations];

    m_curKeyboardHeight = 0;

}
]]>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注