我在我的应用程序的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
您是否尝试过使用UIKeyboardWillShowNotification? –
@EugeneZaychenko这使得它的工作马上耶是谢谢,但有没有什么办法让键盘开启和关闭的速度动画视图?因为现在它只是跳到那个位置,然后键盘就迎面而来。 –
在self.textViewPOS.constant = keyboardSize.height下面过滤此代码; [UIView animateWithDuration:0.25动画:^ {self.view layoutIfNeeded]; }完成:^(BOOL完成){ }]; –