UIViewControllerTransitioningDelegate使用NSLayoutConstraint问题解除动画

时间:2013-11-14 09:37:22

标签: ios animation autolayout

1)我有一个任务是使用自定义动画来呈现和解除模态UIViewController。

2)自定义动画是更改alpha并移动一个子元素

3)我创建了FadeInAnimationControllerFadeOutAnimationController类来实现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,它适用于当前动画

5)问题:

对于Dismiss动画NSLayoutConstraint不起作用,所以我必须使用Frame做同样的事情并且它有效。 AutoLayout和iOS7不是很好,但由于我需要忽略这个视图,所以我不关心它的自动布局。

所以问题是为什么NSLayoutConstraint方法不起作用???我在animateTransition中记录了约束:

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

他们仍在场。

1 个答案:

答案 0 :(得分:0)

不要在动画块中设置自动布局常量。

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

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