0

我有两个 UIViews 和 UIView2 有 UITextView 可以放置在用户喜欢添加文本标签的任何地方。当用户将 UiTextView 放在底部时,键盘出现在输入文本并且 UITextView 向上移动。而且效果很好!我还需要移动 UIView1 下的 UIView1。

UIView2 是 UIView1 的属性,当 UIView2 的 UITextView 成为FirstResponder 时,我需要通知 UIView1 为其执行 Move Up 方法。

 [[NSNotificationCenter defaultCenter] addObserver:self.drawingView
                                         selector:@selector(keyboardWillShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self.drawingView
                                         selector:@selector(keyboardWillHide)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
4

1 回答 1

2

您已经为键盘事件添加了通知,那么您要做的就是实现方法keyboardWillShow 和keyboardWillHide。见下文

 - (void)keyboardWillShow:(NSNotification *)notification {
/*
 Reduce the size of the text view so that it's not obscured by the keyboard.
 Animate the resize so that it's in sync with the appearance of the keyboard.
 */
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[self moveCommentBarViewWithKeyBoardHeight:keyboardRect.size.height withDuration:animationDuration];

}

 - (void)keyboardWillHide:(NSNotification *)notification {
   NSDictionary* userInfo = [notification userInfo];
/*
   Restore the size of the text view (fill self's view).
 Animate the resize so that it's in sync with the disappearance of the keyboard.
 */
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[self moveCommentBarViewWithKeyBoardHeight:0 withDuration:animationDuration];
}

希望它可以帮助:)

PS:添加此通知时,最好添加到方法 -(void)viewWillAppear:(BOOL)animite。并使用

 [[NSNotificationCenter defaultCenter] removeObserver:self]; 

在 viewWillDisappear 时移除观察。

 -(void)moveCommentBarViewWithKeyBoardHeight:(CGFloat)kHeighy withDuration:(NSTimeInterval)animationD
 {
 CGRect tempRect = commentEditedBarView.frame;
 [UIView beginAnimations:@"Animation" context:nil];
 [UIView setAnimationDuration:animationD];

 [commentEditedBarView setFrame:CGRectMake(tempRect.origin.x, self.view.frame.size.height-kHeighy-tempRect.size.height, tempRect.size.width, tempRect.size.height)];
 [UIView commitAnimations];

 }
于 2013-06-20T15:01:42.797 回答