我想在 presentViewController 期间制作动画:

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *toView = toVC.view;
    UIView *fromView = fromVC.view;
    UIView* containerView = [transitionContext containerView];
    [containerView addSubview:toView];
    NSTimeInterval duration = 0.35;
    [UIView transitionFromView:fromView toView:toView duration:duration options:(self.reverse?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight) |
    UIViewAnimationOptionAllowUserInteraction completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

这在 iOS7 上运行良好,但在 iOS8 上没有动画。

最佳答案

+transitionFromView:toView:duration:options:completion: 负责处理 View 层次结构。你不应该需要 [containerView addSubview:toView]
+transitionFromView:toView:duration:options:completion: 内部的某处应该是以下代码:

UIView *superview = fromView.superview;
[fromView removeFromSuperview];
[superview addSubview:toView];

您是否尝试删除 containerView 代码:
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *toView = toVC.view;
    UIView *fromView = fromVC.view;
    NSTimeInterval duration = 0.35;
    [UIView transitionFromView:fromView toView:toView duration:duration options:(self.reverse?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight) |
    UIViewAnimationOptionAllowUserInteraction completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

关于objective-c - 为什么 iOS7 的 animateTransition 中的 UIView transitionFromView 可以,但 iOS8 没有动画,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24969916/

10-12 02:43