Дисплей клавиатуры без анимации - PullRequest
21 голосов
/ 05 декабря 2009

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

Ответы [ 7 ]

53 голосов
/ 05 декабря 2009

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

, а затем то же самое для UIKeyboardWillHideNotification/UIKeyboardDidHideNotification уведомлений.

9 голосов
/ 09 октября 2014

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];
}
7 голосов
/ 24 марта 2017

Я нашел лучшее решение, используя UIView.setAnimationsEnabled(_ enabled: Bool).

Swift 3

UIView.setAnimationsEnabled(false)
textField.becomeFirstResponder()
// or textField.resignFirstResponder() if you want to dismiss the keyboard
UIView.setAnimationsEnabled(true)
7 голосов
/ 02 марта 2017

Попробуйте

[UIView performWithoutAnimation:^{
    [textField becomeFirstResponder];
}];
3 голосов
/ 13 декабря 2016

Ответ @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)
    }
0 голосов
/ 01 апреля 2015

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

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

    [txtFirstName becomeFirstResponder];
}

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

    [txtFirstName resignFirstResponder];
}
0 голосов
/ 06 января 2015

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...