2016-05-17 42 views
1

我在我的應用程序的textview上使用以下代碼。我做到了這一點,當鍵盤觸發視圖將在鍵盤上方,當我點擊鍵盤和文本視圖之外時,它會關閉鍵盤。我遇到的問題是當我點擊textview有一個延遲的textview在鍵盤上移動大約5秒鐘然後它彈出,當我點擊關閉鍵盤時,它立即消失,不會動畫像鍵盤確實打開。有沒有什麼辦法可以解決這個問題,併爲視圖添加動畫,以便在鍵盤上移動,因爲它可以打開和關閉而不是跳躍?目標C查看不能隨鍵盤移動

感謝

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 
    [center addObserver:self selector:@selector(didShow:) name:UIKeyboardDidShowNotification object:nil]; 
    [center addObserver:self selector:@selector(didHide) name:UIKeyboardWillHideNotification object:nil]; 


     _theTextView.delegate = self; 
    _theTextView.text = @"Type your message.."; 
    _theTextView.textColor = [UIColor lightGrayColor]; //optional 
    _theTextView.layer.cornerRadius = 5; 
    _theTextView.layer.masksToBounds = YES; 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
            initWithTarget:self 
            action:@selector(dismissKeyboard)]; 

    [self.view addGestureRecognizer:tap]; 

} 

-(void)dismissKeyboard { 
    [_theTextView resignFirstResponder]; 
} 
- (void)didShow:(NSNotification *)notification 
{ 
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    self.textViewPOS.constant = keyboardSize.height; 

    NSLog(@"Opened"); 
} 

- (void)didHide 
{ 
    self.textViewPOS.constant = 0; 
    NSLog(@"Closed"); 
} 

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 
    NSLog(@"typing"); 
    return YES; 
} 
- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    if ([textView.text isEqualToString:@"Type your message.."]) { 
     textView.text = @""; 
     textView.textColor = [UIColor blackColor]; //optional 
    } 
    [textView becomeFirstResponder]; 
} 
- (void)textViewDidEndEditing:(UITextView *)textView 
{ 
    if ([textView.text isEqualToString:@""]) { 
     textView.text = @"Type your message.."; 
     textView.textColor = [UIColor lightGrayColor]; //optional 
    } 
    [textView resignFirstResponder]; 
} 

@end 
+0

您是否嘗試過使用UIKeyboardWillShowNotification? –

+1

@EugeneZaychenko這使得它的工作馬上耶是謝謝,但有沒有什麼辦法讓鍵盤開啓和關閉的速度動畫視圖?因爲現在它只是跳到那個位置,然後鍵盤就迎面而來。 –

+0

在self.textViewPOS.constant = keyboardSize.height下面過濾此代碼; [UIView animateWithDuration:0.25動畫:^ {self.view layoutIfNeeded]; }完成:^(BOOL完成){ }]; –

回答

2

對於您可以使用UIKeyboardWillShowNotification尤金Zaychenko延遲問題建議。它會解決你的問題。

[center addObserver:self selector:@selector(willShow:) name:UIKeyboardWillShowNotification object:nil]; 

對於動畫使用下面的代碼

- (void)didHide 
{ 
    self.textViewPOS.constant = 0; 
    NSLog(@"Closed"); 

    [UIView animateWithDuration:1 delay:0 options:0 animations:^{ 

     self.textViewPOS.constant = 0; 
     [self.view layoutIfNeeded]; 

    } completion:nil]; 

} 

- (void)willShow:(NSNotification *)notification 
{ 
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    [UIView animateWithDuration:1 delay:0 options:0 animations:^{ 

     self.textViewPOS.constant = keyboardSize.height; 
     [self.view layoutIfNeeded]; 

    } completion:nil]; 

    NSLog(@"Opened"); 
} 

讓我知道如果任何查詢。

1

只有當您第一次執行您的項目時纔會發生這種延遲嗎?

  1. 然後在應用程序中用選項完成啓動(在appcartigate中)只需使用代碼創建一個textview即可。
  2. 然後將其添加到我們的視圖中。然後將textview作爲第一響應者。
  3. 然後辭去它作爲第一響應者..
  4. 然後最後從我們的超級視圖中刪除textview。

您的延遲問題將由此解決。