显示没有动画的键盘

sam*_*tte 21 iphone keyboard

看着,UIKeyboardAnimationDurationUserInfoKey但我无法找到任何地方如何将其设置为自定义值.

mon*_*ker 53

UIKeyboardAnimationDurationUserInfoKey 是保存动画持续时间的字典键的const字符串标识符,因此无法轻松更改它.

使键盘在没有动画的情况下显示的一种方法是观察键盘通知并在即将出现时禁用动画,然后重新启用它们.当然,这也会禁用任何其他动画.

[[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];
}
Run Code Online (Sandbox Code Playgroud)

然后是相同的UIKeyboardWillHideNotification/UIKeyboardDidHideNotification通知.

  • 它不再适用于iOS 8(种子3). (7认同)

Vad*_*off 9

iOS8上的兼容:

添加适当的委托方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [UIView setAnimationsEnabled:NO];
}
Run Code Online (Sandbox Code Playgroud)

要么

- (void)textViewDidBeginEditing:(UITextView *)textView {
    [UIView setAnimationsEnabled:NO];
}
Run Code Online (Sandbox Code Playgroud)

添加键盘通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

方法:

- (void)didShowKeyboard:(NSNotification *)notification {
    [UIView setAnimationsEnabled:YES];
}
Run Code Online (Sandbox Code Playgroud)


goe*_*etz 7

尝试

[UIView performWithoutAnimation:^{
    [textField becomeFirstResponder];
}];
Run Code Online (Sandbox Code Playgroud)


Sao*_*wan 7

我发现最好的解决方案是使用UIView.setAnimationsEnabled(_ enabled: Bool).

斯威夫特3

UIView.setAnimationsEnabled(false)
textField.becomeFirstResponder()
// or textField.resignFirstResponder() if you want to dismiss the keyboard
UIView.setAnimationsEnabled(true)
Run Code Online (Sandbox Code Playgroud)