Подтвердить что ты не робот

Отображать клавиатуру без анимации

Посмотрел на UIKeyboardAnimationDurationUserInfoKey, но я просто не могу найти, как установить его на пользовательское значение.

4b9b3361

Ответ 1

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.

Ответ 2

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];
}

Ответ 3

Я нашел, что лучшим решением является UIView.setAnimationsEnabled(_ enabled: Bool).

Swift 3

UIView.setAnimationsEnabled(false)
textField.becomeFirstResponder()
// or textField.resignFirstResponder() if you want to dismiss the keyboard
UIView.setAnimationsEnabled(true)

Ответ 4

Ответ @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

Try

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

Ответ 6

Мне пришлось отключить анимацию в textViewShouldBeginEditing, textFieldDidBeginEditing не работало для меня (iOS 8)

Ответ 7

Это довольно простые парни. Не используйте UIView: SetAnimationEnabled, поскольку это будет потенциально неприятным. Вот как я удаляю анимацию при показе клавиатуры.

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

    [txtFirstName becomeFirstResponder];
}

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

    [txtFirstName resignFirstResponder];
}