0

Использую анимацию для смещения объекта при выезде клавиатуры, проблема в следующем когда первый раз захожу в контроллер и выдвигаю клавиатуру, метод нотификации UIKeyboardWillChangeFrameNotification

Выдает следующие параметры

    UIKeyboardAnimationCurveUserInfoKey = 7;
    UIKeyboardAnimationDurationUserInfoKey = "0.25";
    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 253}}";
    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 694.5}";
    UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 441.5}";
    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 253}}";
    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 315}, {320, 253}}";
    UIKeyboardIsLocalUserInfoKey = 1;

Как только я возвращаюсь назад и опять захожу в этот контроллер параметры меняются на

UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 253}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {0, 410.5}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {0, 157.5}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{-160, 284}, {320, 253}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{-160, 31}, {320, 253}}";
UIKeyboardIsLocalUserInfoKey = 1;

После этого объект уезжает куда то вверх, и что самое интересное после этого опять вызывается метод нотификации, с правильными параметрами.

UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 253}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {0, 157.5}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 441.5}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{-160, 31}, {320, 253}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 315}, {320, 253}}";
UIKeyboardIsLocalUserInfoKey = 1;

Переход осуществляю через presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion

Возврат производится через dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion

0

1 ответ 1

1

Подпишись на нотификации:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRecieveNotification:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRecieveNotification:) name:UIKeyboardWillHideNotification object:nil];

Далее метод, на который попадаешь по селектору:

- (void)didRecieveNotification:(NSNotification *)notification {
    if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) {
        [self keyboardWillShowWithDuration:[self animationDurationFromNotification:notification]];
    } else if ([notification.name isEqualToString:UIKeyboardWillHideNotification]) {
        [self keyboardWillHideWithDuration:[self animationDurationFromNotification:notification]];
    }
}

После меняешь constraint в зависимости от твоих задач:

- (double)animationDurationFromNotification:(NSNotification *)notification {
    NSDictionary *info = [notification userInfo];
    NSNumber *number = info[UIKeyboardAnimationDurationUserInfoKey];
    return [number doubleValue];
}

- (void)keyboardWillShowWithDuration:(double)duration {
    _topAlignConstraint.constant = //set constant here
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:duration
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         [weakSelf.view layoutIfNeeded];
                     }
                     completion:nil];
}

- (void)keyboardWillHideWithDuration:(double)duration {
    __weak typeof(self) weakSelf = self;
    _topAlignConstraint.constant = //set constant here
    [UIView animateWithDuration:duration
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         [weakSelf.view layoutIfNeeded];
                     }
                     completion:nil];
}

P.S. Либо можешь найти готовое решение.

P.S.S. И не забудь отписаться от нотификаций.

1

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.