我在我的應用程序的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完成){ }]; –