2016-05-17 15 views
1

私のアプリケーションのテキストビューで次のコードを使用しています。私はキーボードがキーボードの上に表示され、キーボードとテキストビューの外側をクリックすると、キーボードが閉じられるようにキーボードをトリガーすると、そのようにしました。私が抱えている問題は、テキストビューをクリックするとテキストビューの遅延が約5秒間キーボードの上を移動し、ポップアップし、キーボードを閉じるとすぐに消えてしまい、すぐに消えてしまいますキーボードが開きます。私はこれを修正し、ジャンプの代わりに開閉するので、キーボードの上を移動するビューをアニメーション化する方法はありますか?ユージンZaychenkoが示唆するようおUIKeyboardWillShowNotification使用できる遅延の問題については目的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

。あなたの問題を解決します。コード

- (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"); 
} 

任意のクエリなら、私に教えてください、次のアニメーション用の

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

1

この遅延は、プロジェクトを最初に実行したときにのみ発生しますか?

  1. その後、アプリケーションで(appdeligateで)オプションを使用して起動しましたが、単にコードを使用してテキストビューを作成しました。
  2. 次に、私たちのビューを追加します。次に、textviewを最初のレスポンダにします。
  3. その後、最初のレスポンダとして辞任してください。
  4. 最後に、スーパービューからテキストビューを削除します。

あなたの遅延の問題はこれで解決されます。

関連する問題