显示没有动画的键盘

时间:2009-12-05 08:17:35

标签: iphone keyboard

查看UIKeyboardAnimationDurationUserInfoKey,但我找不到任何地方如何将其设置为自定义值。

7 个答案:

答案 0 :(得分:53)

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/UIKeyboardDidHideNotification通知也一样。

答案 1 :(得分:9)

iOS8上兼容的:

添加适当的委托方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [UIView setAnimationsEnabled:NO];
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    [UIView setAnimationsEnabled:NO];
}

添加键盘通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];

方法:

- (void)didShowKeyboard:(NSNotification *)notification {
    [UIView setAnimationsEnabled:YES];
}

答案 2 :(得分:7)

尝试

[UIView performWithoutAnimation:^{
    [textField becomeFirstResponder];
}];

答案 3 :(得分:6)

我发现最好的解决方案是使用UIView.setAnimationsEnabled(false) textField.becomeFirstResponder() // or textField.resignFirstResponder() if you want to dismiss the keyboard UIView.setAnimationsEnabled(true)

Swift 3

print

答案 4 :(得分:3)

@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)
    }

答案 5 :(得分:-1)

我必须在textViewShouldBeginEditing中禁用动画,textFieldDidBeginEditing对我不起作用(iOS 8)

答案 6 :(得分:-5)

这很简单。不要使用UIView:SetAnimationEnabled,因为这可能会很麻烦。这是我在键盘显示时删除动画的方法。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [txtFirstName becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [txtFirstName resignFirstResponder];
}