UIViewControllerTransitioningDelegate отключает анимацию с проблемой NSLayoutConstraint - программирование

UIViewControllerTransitioningDelegate отключает анимацию с проблемой NSLayoutConstraint

1) У меня есть задача представить и отклонить модальный UIViewController с пользовательской анимацией.

2) Пользовательская анимация - это изменение альфы и перемещение одного дочернего элемента

3) Я создал классы FadeInAnimationController и FadeOutAnimationController для реализации UIViewControllerAnimatedTransitioning следующим образом:

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    // obtain state from the context
    CIToViewController *toViewController = (CIToViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    // obtain the container view
    UIView *containerView = [transitionContext containerView];

    // set the intial state
    toViewController.view.alpha = 0.0f;
    toViewController.elementBottomPosition.constant -= 20.0f;
    [toViewController.view layoutIfNeeded];

    // add the view
    [containerView addSubview:toViewController.view];

    // animate
    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                     animations:^{
                         toViewController.view.alpha = 1.0f;
                         toViewController.elementBottomPosition.constant += 20.0f;
                         [toViewController.view layoutIfNeeded];
                     }
                     completion:^(BOOL finished) {
                         [transitionContext completeTransition:YES];
                     }];
     }

4) elementBottomPosition является NSLayoutConstraint и отлично работает для анимации Present

5) ПРОБЛЕМА:

Для Dismiss анимации NSLayoutConstraint не работает, поэтому я должен был сделать то же самое, используя Frame, и он сработал. Это не очень хорошо с AutoLayout и iOS7, но поскольку мне нужно отклонить эту точку зрения, мне не важно ее автоопределение.

Итак, вопрос в том, почему NSLayoutConstraint не работает? Я зарегистрировал ограничения в animateTransition:

NSLog(@"constraints %@", fromViewController.view.constraints);

И они все еще присутствуют.

4b9b3361

Ответ 1

Не устанавливайте постоянные макеты в блоке анимации.

toViewController.elementBottomPosition.constant -= 20.0f;
[self.view layoutIfNeeded];
toViewController.elementBottomPosition.constant += 20.0f;

//Animation block here ^{
[self.view layoutIfNeeded];
}