前言因此,我有一个带有聊天部分的应用程序,并且正在将隐藏和显示键盘的动画与聊天输入的上升和下降同步。这是我正在使用的代码:展示:- (void) keyboardWillShow:(NSNotification *)note { NSDictionary *keyboardAnimationDetail = [note userInfo]; UIViewAnimationCurve animationCurve = [keyboardAnimationDetail[UIKeyboardAnimationCurveUserInfoKey] integerValue]; CGFloat duration = [keyboardAnimationDetail[UIKeyboardAnimationDurationUserInfoKey] floatValue]; NSValue* keyboardFrameBegin = [keyboardAnimationDetail valueForKey:UIKeyboardFrameBeginUserInfoKey]; CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue]; // working for hardware keyboard //UIViewAnimationOptions options = (UIViewAnimationOptions)animationCurve; // working for virtual keyboard UIViewAnimationOptions options = (animationCurve << 16); [UIView animateWithDuration:duration delay:0.0 options:options animations:^{ textView.frame = CGRectMake(0, self.view.bounds.size.height - keyboardFrameBeginRect.size.height, self.view.bounds.size.width, -40); } completion:nil];}隐藏:- (void) keyboardWillHide:(NSNotification *)note { NSDictionary *keyboardAnimationDetail = [note userInfo]; UIViewAnimationCurve animationCurve = [keyboardAnimationDetail[UIKeyboardAnimationCurveUserInfoKey] integerValue]; CGFloat duration = [keyboardAnimationDetail[UIKeyboardAnimationDurationUserInfoKey] floatValue]; // hardware keyboard //UIViewAnimationOptions options = (UIViewAnimationOptions)animationCurve; // virtual keyboard UIViewAnimationOptions options = (animationCurve << 16); [UIView animateWithDuration:duration delay:0.0 options:options animations:^{ textView.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, -40); } completion:nil];}这对于虚拟键盘非常有效,但是如果由于断开或连接硬件键盘而调用keyboardWillShow:或keyboardWillHide:,则动画会滞后。我可以通过更改UIViewAnimationOptions来解决此问题代替:// Works with virtual keyboardUIViewAnimationOptions options = (animationCurve << 16);和:// working for firstResponder keyboardUIViewAnimationOptions options = (UIViewAnimationOptions)animationCurve;但是有了这个,现在virtualKeyboard动画滞后了我意识到硬件键盘动画并不是很普遍,它也许不是最重要的问题,但是我喜欢一切都能正常工作! 例子带(animationCurve 带(UIViewAnimationOptions)animationCurve的VirtualKeyboard- splinter 带(animationCurve 带(UIViewAnimationOptions)animationCurve的HardwareKeyboard-工作 笔记在模拟器cmd + shft + k中模拟硬件键盘是的,这可以在真实设备上复制。如果需要,这是我的其余代码,仅用于复制添加文字 View textView = [UITextView new];textView.layer.borderWidth = 10;textView.layer.borderColor = [UIColor blackColor].CGColor;textView.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, -40);[self.view addSubview:textView];UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];[tap addTarget:self action:@selector(handleTap:)];[self.view addGestureRecognizer:tap];// OBSERVE KEYBOARD[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];手柄丝锥:- (void) handleTap:(UITapGestureRecognizer *)tap { NSLog(@"tapped"); [textView resignFirstResponder];} 问题:这是怎么回事,有没有什么好方法可以获得一致的动画,而与虚拟/硬件键盘无关?我知道这很长,谢谢您的阅读! (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 由于Apple在键盘通知中向您发送的动画曲线没有相应的UIViewAnimationOption位,因此您需要下拉至老式的无块动画并直接使用该曲线:NSTimeInterval duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];UIViewAnimationCurve curve = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];[UIView beginAnimations:@"SomeAnimationID" context:NULL];[UIView setAnimationCurve:curve];[UIView setAnimationDuration:duration];// Animation code[UIView commitAnimations]; (adsbygoogle = window.adsbygoogle || []).push({}); 10-08 14:15