同时关闭带有视图的键盘

时间:2014-09-24 02:14:20

标签: ios ios7 ios8 xcode6

我正在构建的应用程序有一个消息/聊天视图控制器,其中包含一个带有textView的视图和一个位于视图底部的按钮(标签栏设置为隐藏)。加载视图时,包含这些对象的视图(textView和按钮)位于屏幕的底部(就像进入对话时在iMessage应用程序中的显示方式)

现在当点击文本视图时,显然键盘向上移动,并且我已经设法让视图向上移动并坐在键盘顶部(就像你输入一个新键盘一样)短信)。

现在我要做的是用键盘让视图上下移动。截至目前,它从底部开始。当键盘上升时,该视图会在底部停留一两秒,然后只显示在键盘上方。键盘解除时也是如此。

我希望我可以提供图片,但我想我的名声并没有完全削减它

有没有办法可以匹配两者的过渡或动画,以便它们一起移动,就像它们被连接一样?

视图已初始化

//Create the keyboard UIView
    self.keyboardView =  [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.origin.y + self.view.frame.size.height - (self.navigationController.tabBarController.tabBar.frame.size.height), self.view.frame.size.width, 50)];

用于移动视图的选择器

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameDidChange:) name:UIKeyboardDidChangeFrameNotification object:nil];

实施选择

- (void)keyboardFrameDidChange:(NSNotification*)notification{
    NSDictionary* info = [notification userInfo];

    CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        [self.keyboardView setFrame:CGRectMake(0, kKeyBoardFrame.origin.y - self.keyboardView.frame.size.height, 320, self.keyboardView.frame.size.height)];
        self.myScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.myTableView.frame.size.height);


}

3 个答案:

答案 0 :(得分:1)

你可以在这里做的是键盘通知

[[NSNotificationCenter defaultCenter] addObserver:self

                                         selector:@selector(keyboardWasShown:)

                                             name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self

                                         selector:@selector(keyboardWillBeHidden:)

                                             name:UIKeyboardWillHideNotification object:nil];

<强> keyboardWasShown

- (void)keyboardWasShown:(NSNotification*)aNotification

{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
double duration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
int curve = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView beginAnimations:@"foo" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
CGRect rect=self.viewChatWindow.frame;
rect.size.height=self.viewChatWindow.frame.size.height-kbSize.height;
self.viewChatWindow.frame=rect;

[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(scrollToLast)];
[UIView commitAnimations];

}

<强> keyboardWillBeHidden

- (void)keyboardWillBeHidden:(NSNotification*)aNotification

{
double duration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
int curve = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView beginAnimations:@"foo" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
CGRect rect=self.viewChatWindow.frame;
rect.size.height=self.view.frame.size.height-rect.origin.y;
self.viewChatWindow.frame=rect;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(scrollToLast)];
[UIView commitAnimations];
}

答案 1 :(得分:0)

将[keyboardView setFrame:...]包装在动画块中。您可以从通知对象中获取键盘的动画持续时间和曲线:

NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration delay:0 options:curve animations:^{
    self.keyboardView.frame = ...;
} completion:nil];

答案 2 :(得分:0)

使用UITextFieldDelegate

这将是键盘将出现的旗帜

-(void)textFieldDidBeginEditing:(UITextField *)textField{
//Animate like slide up.
        [UIView animateWithDuration:0.3
                         animations:^{
                             //Use offset rather than contentSize
                             scrollView.contentOffset = CGPointMake(x,y);
                         }];
}

当你的键盘被解雇时(resignFirstResponder)

-(void)dismissKeyboard{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.3
                     animations:^{
                         scrollView.contentOffset = CGPointMake(0,0);

                     }];
 }