2014-09-23 22 views
0

我有查看鍵入消息,如下所示。移動視圖以同時重置框架和鍵盤

enter image description here

現在,當I型消息,鍵盤出現並且示於下面的圖中的框應當移動的正上方的鍵盤。

enter image description here

我的問題 他們的鍵盤動畫和觀看動畫出現在不同的時間。鍵盤出現,然後出現視圖。即使我試圖將動畫時間設置爲任何時間,它們都會在不同的時間出現。

我該如何解決我的問題?

請建議我解決它的方式,以便鍵盤和視圖動畫顯示,如果他們是相同的看法。兩個動畫都應該在確切的時間發生,以便它們看起來像是同時出現的相同視圖。

我試圖

我的看法沒有負載有以下代碼

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

現在鍵盤顯示功能看起來像

- (void)keyboardWillShow:(NSNotification *)note{ 
    NSDictionary* keyboardInfo = [note userInfo]; 
    CGFloat duration = [[keyboardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; 
[UIView animateWithDuration:duration animations: 
^{ 
    //chat_typingView is name for typing view 
    chat_typingView.frame = CGRectMake(chat_typingView.frame.origin.x, 
               238, 
               chat_typingView.frame.size.width, 
               chat_typingView.frame.size.height); 
} 

回答

0

也許你應該檢查一下你的chat_typinfView是第一個響應之前做動畫和禁用Autolayout(非常重要)。

if ([chat_typingView isFirstResponder]) { 
    // Do the animation 
} 

PS是推薦訂閱通知上viewWillApper而不是viewDidLoad中

0

我有一個類似的設置我的應用程序之一,我做了以下內容:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

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

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardHidden:) 
               name:UIKeyboardWillHideNotification object:nil]; 
} 

- (void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 

    /*** FOR AUTOLAYOUT MODIFICATIONS & ADDITIONS @ RUNTIME ***/ 

    self.defaultViewFrame = self.myView.frame 
} 


- (void) keyboardShowed:(NSNotification*)notification { 

    //GET KEYBOARD FRAME 
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    //CONVERT KEYBOARD FRAME TO MATCH THE OUR COORDINATE SYSTEM (FOR UPSIDEDOWN ROTATION) 
    CGRect convertedFrame = [self.view convertRect:keyboardFrame fromView:self.view.window]; 

    //..... do something with the convertedFrame (in your case convertedFrame.origin.y) 
} 

- (void) keyboardHidden:(NSNotification*)notification { 

    //RESTORE ORIGINAL STATE 

    [UIView transitionWithView:self.view 
         duration:.3f 
         options:UIViewAnimationOptionCurveLinear 
        animations:^{ 
         self.myView.frame = self.defaultViewFrame; 
        } 
         completion:nil]; 
} 
相關問題