2016-05-17 17 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。

您的延迟问题将由此解决。