2014-09-24 77 views
1

我正在構建的應用程序有一個消息/聊天視圖控制器,該視圖帶有一個textView和一個位於視圖底部的按鈕(標籤欄設置爲隱藏)。當視圖加載時,包含這些對象(textView和按鈕)的視圖位於屏幕的底部(例如,當您進入對話時它在iMessage應用程序中的外觀)同時關閉鍵盤與視圖

現在當textview很明顯,鍵盤向上移動,隨着我設法讓視圖向上移動並坐在鍵盤的頂部(例如,當你要輸入新的文本信息時)。

現在我想要做的是讓視圖與鍵盤上下移動。截至目前它始於底部。當鍵盤上升時,該視圖會停留在底部一兩秒鐘,然後出現在鍵盤上方。當鍵盤被解僱時也是如此。

我希望我能提供的圖片,但我想我的名聲不太削減IT

有沒有一種方法,我可以匹配兩個讓他們一起移動的過渡或動畫,就好像它們連接?

視圖被初始化

//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); 


} 

回答

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]; 
+0

我使用這個方法如果關閉該鍵盤'self.myScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive ;' – kyco16 2014-09-24 03:13:57

+0

嗯,好問題。我對UIScrollViewKeyboardDismissModeInteractive不是很熟悉。看起來像這可能是http://stackoverflow.com/questions/19118921/moving-a-bar-with-uiscrollviewkeyboarddismissmodeinteractive的副本。您是否嘗試過那裏描述的解決方案? – ryanipete 2014-09-24 15:12:13

+0

我已經嘗試過,如果我用其自然動畫解散鍵盤,它會很好地工作。所以謝謝! – kyco16 2014-09-24 17:03:32

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); 

        }]; 
} 
+0

但我正在使用textView ...如果我正在使用文本字段,這不是理想嗎? – kyco16 2014-09-24 03:03:23

+0

我認爲這只是與textview相同,但UITextViewDelegate。 – Teffi 2014-09-24 03:25:30

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]; 
}