ios输入框随键盘上下移动动画


#pragma mark - keyboard up and down
 
 - (void)addKeyboardEvents
 {
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
 }
 
 - (void)removeKeyboardEvents
 {
     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
 }
 

- (void)keyboardWillShow:(NSNotification *)notification
 {
     NSDictionary *userInfo = [notification userInfo];
     
     NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
     
     CGRect keyboardRect = [aValue CGRectValue];
     keyboardRect = [self convertRect:keyboardRect fromView:nil];
     
     CGFloat keyboardTop = keyboardRect.size.height;
     CGRect newTextViewFrame = self.bgView.frame;
     newTextViewFrame.origin.y = keyboardTop - self.bgView.frame.origin.y;
     
     NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
     NSTimeInterval animationDuration;
     [animationDurationValue getValue:&animationDuration];
     
     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:animationDuration];
     
     self.bgView.frame = newTextViewFrame;
     
     [UIView commitAnimations];
 }
 
 - (void)keyboardWillHide:(NSNotification *)notification
 {
     NSDictionary* userInfo = [notification userInfo];
     
     NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
     NSTimeInterval animationDuration;
     [animationDurationValue getValue:&animationDuration];
     
     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:animationDuration];
     
     [self.bgView setFrame:CGRectMake(self.bgView.frame.origin.x, IPAD_2_3_HEIGHT/2 - 424/4, self.bgView.frame.size.width, self.bgView.frame.size.height)];
     
     [UIView commitAnimations];
 }



你可能感兴趣的:(ios输入框随键盘上下移动动画)