1) 我有一个任务,需要用自定义动画呈现和关闭模态视图控制器。
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);
它们仍然存在。
animations:
块之前,消失的控制器上的viewWillDisappear
和viewDidDisappear
是否被调用了吗? - Arek Holko