Iphone 显示没有动画的键盘
查看了Iphone 显示没有动画的键盘,iphone,keyboard,Iphone,Keyboard,查看了UIKeyboardAnimationDurationUserInfoKey,但我在任何地方都找不到如何将其设置为自定义值。UIKeyboardAnimationDurationUserInfoKey是保存动画持续时间的字典键的常量字符串标识符,因此无法轻松更改 使键盘在没有动画的情况下显示的一种方法是观察键盘通知,并在其即将显示时禁用动画,然后重新启用它们。当然,这也会禁用任何其他动画 [[NSNotificationCenter defaultCenter] addObserver:s
UIKeyboardAnimationDurationUserInfoKey
,但我在任何地方都找不到如何将其设置为自定义值。UIKeyboardAnimationDurationUserInfoKey
是保存动画持续时间的字典键的常量字符串标识符,因此无法轻松更改
使键盘在没有动画的情况下显示的一种方法是观察键盘通知,并在其即将显示时禁用动画,然后重新启用它们。当然,这也会禁用任何其他动画
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willShowKeyboard:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didShowKeyboard:)
name:UIKeyboardDidShowNotification
object:nil];
- (void)willShowKeyboard:(NSNotification *)notification {
[UIView setAnimationsEnabled:NO];
}
- (void)didShowKeyboard:(NSNotification *)notification {
[UIView setAnimationsEnabled:YES];
}
然后同样适用于UIKeyboardWillHideNotification/UIKeyboardDidenotification
通知。iOS8兼容:
添加适当的委托方法:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView setAnimationsEnabled:NO];
}
或
添加键盘通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];
方法:
- (void)didShowKeyboard:(NSNotification *)notification {
[UIView setAnimationsEnabled:YES];
}
我不得不在
textview shouldbeginediting
中禁用动画,textfielddeginediting
对我不起作用(iOS 8)这很简单。不要使用UIView:SetAnimationEnabled,因为这可能会带来麻烦。这是我在键盘显示时删除动画的方式
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[txtFirstName becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[txtFirstName resignFirstResponder];
}
@Vadoff的答案非常完美。此处为Swift 3:
override func viewDidLoad() {
super.viewDidLoad()
//...
// Add observer to notificationCenter so that the method didShowKeyboard(_:) is called when the keyboard did show.
NotificationCenter.default.addObserver(self, selector: #selector(type(of: self).didShowKeyboard(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
// Make textField the first responder.
textField.becomeFirstResponder() // <- Change textField to the name of your textField.
}
func textFieldDidBeginEditing(_ textField: UITextField) {
// Disable animations.
UIView.setAnimationsEnabled(false)
}
func didShowKeyboard(_ notification: Notification) {
// Enable animations.
UIView.setAnimationsEnabled(true)
}
override func viewDidLoad(){
super.viewDidLoad()
//...
//将观察者添加到notificationCenter,以便在键盘确实显示时调用方法didShowKeyboard(:)。
NotificationCenter.default.addObserver(self,选择器:#选择器(类型(of:self).didShowKeyboard(:)),名称:NSNotification.name.UIKeyboardDidShow,对象:nil)
//让textField成为第一响应者。
textField.becomeFirstResponder()//尝试
[UIView性能无动画:^{
[textField成为第一响应者];
}];
我发现最好的解决方案是使用UIView.setanimationEnabled(uuEnabled:Bool)
Swift 3
UIView.setAnimationsEnabled(false)
textField.becomeFirstResponder()
// or textField.resignFirstResponder() if you want to dismiss the keyboard
UIView.setAnimationsEnabled(true)
谢谢!我一直在寻找一个与iOS-8兼容的解决方案来解决这个错误。不知道为什么textfielddibeginediting
修复了它。感谢@vadoff在我的情况下处于活动状态,其他自由是创建问题。提供一个代码示例将使这是一个更好的答案(查看队列中的注释)。这将是一个带有代码的好答案,也可能只是对已接受内容的注释/编辑answer@JonahGabriel你至少能告诉我为什么它是错误的。它已经过测试,而且工作正常。问题是在没有动画的情况下显示键盘,而不仅仅是显示键盘。@JonahGabriel真的是个男人!你认为我没有读这些问题吗选项。它是viewWillApear,所以你看不到动画。在你投票否决一颗善良的心之前,运行代码并阅读代码。你是那些试图让社区垮台的人的一个很好的例子。这与键盘动画无关。你还需要添加开始编辑目标:textField..addTarget(self,action:#选择器(textfielddebeginediting(:)),用于:.editingdidbegen)
UIView.setAnimationsEnabled(false)
textField.becomeFirstResponder()
// or textField.resignFirstResponder() if you want to dismiss the keyboard
UIView.setAnimationsEnabled(true)