1) 我的任务是使用自定义动画呈现和关闭模态 UIViewController。
2)自定义动画是改变alpha并移动一个子元素
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 不是很好,但由于我需要关闭此 View ,所以我不关心它的自动布局。
所以问题是为什么 NSLayoutConstraint 方法不起作用???我在 animateTransition 中记录了约束:
NSLog(@"constraints %@", fromViewController.view.constraints);
他们仍然存在。
最佳答案
不要在动画 block 中设置自动布局常量。
toViewController.elementBottomPosition.constant -= 20.0f;
[self.view layoutIfNeeded];
toViewController.elementBottomPosition.constant += 20.0f;
//Animation block here ^{
[self.view layoutIfNeeded];
}
关于ios - UIViewControllerTransitioningDelegate 使用 NSLayoutConstraint 问题关闭动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19974111/