iOS- 如何以编程方式禁用 unwind segues 中的动画?

iOS- How to disable animations in unwind segues programmatically?

我在 XCode 中创建了一些 unwind segues,默认情况下它们带有我不想要的动画。在 Stack Overflow 中搜索一番后,我确实发现应该有一个复选框,您可以取消选中以禁用动画,但它出现在 Xcode 7 中,我无法升级到。我将如何以编程方式禁用 unwind segue 动画?我目前 运行 XCode 6.4.

让我们将您要放松的控制器命名为 FirstController 和您要关闭的控制器 SecondController。这里也有两种情况:一种是 SecondController 模态呈现,另一种是 UINavigationController 涉及。

Unwind segue resets/ignores modalPresentationStyle,必须对其进行调整。在展开时调用 FirstControllerIBAction 方法中添加以下内容(我假设您调用了此方法 prepareForUnwind):

- (IBAction)prepareForUnwind:(UIStoryboardSegue *)segue {
    //TODO: add right condition if you have to
    UIViewController* secondController = (UIViewController*)segue.sourceViewController;
    secondController.transitioningDelegate = self;
    secondController.modalPresentationStyle = UIModalPresentationCustom;
}

这部分将在 SecondController 被解除模态时起作用。现在,如果两个控制器都位于 UINavigationControllerFirstController 应该成为它的委托:

- (void)viewDidLoad {
    self.navigationController.delegate = self;
}

这将使 UIKit 请求 FirstController 动画对象以进行后续转换。现在我们应该将FirstController修改为return吧。

FirstController.h中:

@interface FirstController : UIViewController<UIViewControllerTransitioningDelegate, UINavigationControllerDelegate>

将相应的代码添加到 FirstController.m,因此它会 return 一些针对这两种情况的动画对象(注意评论):

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    return [Animator new];
}

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController *)fromVC
                                                 toViewController:(UIViewController *)toVC {
    //This will disable ALL animations on particular UINavigationController
    //return nil to use default animation when appropriate
    return [Animator new];
}

现在实现 Animator 本身。它应该确认 UIViewControllerAnimatedTransitioning 协议。

Animator.h中:

@interface Animator : NSObject<UIViewControllerAnimatedTransitioning>

@end

Animator.m中:

@implementation Animator

- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 0;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *controllerToDismiss = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *viewToDismiss = [transitionContext viewForKey:UITransitionContextFromViewKey];
    CGRect frameToDismissTo = [transitionContext finalFrameForViewController:controllerToDismiss];
    viewToDismiss.frame = frameToDismissTo;

    UIViewController *controllerToPresent = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *viewToPresent = [transitionContext viewForKey:UITransitionContextToViewKey];
    CGRect frameToPresentTo = [transitionContext finalFrameForViewController:controllerToPresent];
    viewToPresent.frame = frameToPresentTo;

    [[transitionContext containerView] addSubview:viewToPresent];

    [transitionContext completeTransition:YES];
}

@end

答案基于此post and this so answer(均不是我的)