使用Push转场效果更改RootViewController

7
在我的iOS应用程序中,我需要在应用程序运行过程中更改窗口的rootViewController。因此,当我动态更改rootViewController时,它会在更改之前闪烁视图。但是我想要的是在更改rootViewController时进行平稳的过渡。我已尝试下列方法,但这不是我想要的过渡效果。
[UIView transitionWithView:self.window
                      duration:.8
                       options:UIViewAnimationOptionTransitionCurlUp
                    animations:^{

                        self.window.rootViewController = tabBarControllerMain;
    [self.window makeKeyAndVisible];
                    }
                    completion:NULL];

我希望你能提供类似于导航控制器推出视图的特定转场效果。请问有人可以给我提供实现方法吗?
3个回答

11
基于Hardik Darji的回答,我创建了一个UIWindow扩展,可以通过可配置的动画类型交换rootViewController,并模拟系统动画 - push, pop, present 和 dismiss。
Swift 3。
public enum SwapRootVCAnimationType {
    case push
    case pop
    case present
    case dismiss
}


extension UIWindow {
public func swapRootViewControllerWithAnimation(newViewController:UIViewController, animationType:SwapRootVCAnimationType, completion: (() -> ())? = nil)
{
    guard let currentViewController = rootViewController else {
        return
    }
    
    let width = currentViewController.view.frame.size.width;
    let height = currentViewController.view.frame.size.height;
    
    var newVCStartAnimationFrame: CGRect?
    var currentVCEndAnimationFrame:CGRect?
    
    var newVCAnimated = true
    
    switch animationType
    {
    case .push:
        newVCStartAnimationFrame = CGRect(x: width, y: 0, width: width, height: height)
        currentVCEndAnimationFrame = CGRect(x: 0 - width/4, y: 0, width: width, height: height)
    case .pop:
        currentVCEndAnimationFrame = CGRect(x: width, y: 0, width: width, height: height)
        newVCStartAnimationFrame = CGRect(x: 0 - width/4, y: 0, width: width, height: height)
        newVCAnimated = false
    case .present:
        newVCStartAnimationFrame = CGRect(x: 0, y: height, width: width, height: height)
    case .dismiss:
        currentVCEndAnimationFrame = CGRect(x: 0, y: height, width: width, height: height)
        newVCAnimated = false
    }
    
    newViewController.view.frame = newVCStartAnimationFrame ?? CGRect(x: 0, y: 0, width: width, height: height)
    
    addSubview(newViewController.view)
    
    if !newVCAnimated {
        bringSubview(toFront: currentViewController.view)
    }
    
    UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseOut], animations: {
        if let currentVCEndAnimationFrame = currentVCEndAnimationFrame {
            currentViewController.view.frame = currentVCEndAnimationFrame
        }
        
        newViewController.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
    }, completion: { finish in
        self.rootViewController = newViewController
        completion?()
    })
    
    makeKeyAndVisible()
    }
}

完美,但对于Swift 3建议在枚举中使用小写字母来表示情况。 - Daniil Chuiko
1
我想我发现了一个小bug:在newViewController里的viewWillAppear被调用了两次。 - Sergei S
确实存在一个小bug,它会调用viewWillAppearviewDidAppear两次。 - thedp

6

以下是用Swift编写的代码,用于实现根视图控制器中的Push和Pop动画。

//Declare enum
enum AnimationType{
        case ANIMATE_RIGHT
        case ANIMATE_LEFT
        case ANIMATE_UP
        case ANIMATE_DOWN
    }
// Create Function...

    func showViewControllerWith(newViewController:UIViewController, usingAnimation animationType:AnimationType)
{

    let currentViewController = UIApplication.sharedApplication().delegate?.window??.rootViewController
    let width = currentViewController?.view.frame.size.width;
    let height = currentViewController?.view.frame.size.height;

    var previousFrame:CGRect?
    var nextFrame:CGRect?

    switch animationType
    {
        case .ANIMATE_LEFT:
            previousFrame = CGRectMake(width!-1, 0.0, width!, height!)
            nextFrame = CGRectMake(-width!, 0.0, width!, height!);
        case .ANIMATE_RIGHT:
            previousFrame = CGRectMake(-width!+1, 0.0, width!, height!);
            nextFrame = CGRectMake(width!, 0.0, width!, height!);
        case .ANIMATE_UP:
            previousFrame = CGRectMake(0.0, height!-1, width!, height!);
            nextFrame = CGRectMake(0.0, -height!+1, width!, height!);
        case .ANIMATE_DOWN:
            previousFrame = CGRectMake(0.0, -height!+1, width!, height!);
            nextFrame = CGRectMake(0.0, height!-1, width!, height!);
    }

    newViewController.view.frame = previousFrame!
    UIApplication.sharedApplication().delegate?.window??.addSubview(newViewController.view)
    UIView.animateWithDuration(0.33,
        animations: { () -> Void in
        newViewController.view.frame = (currentViewController?.view.frame)!
            currentViewController?.view.frame = nextFrame!

        })
        { (fihish:Bool) -> Void in
            UIApplication.sharedApplication().delegate?.window??.rootViewController = newViewController
        }
}


// call the func for ANIMATE_LEFT or ANIMATE_RIGHT etc

     self.showViewControllerWith(rootViewController, usingAnimation: AnimationType.ANIMATE_LEFT)

希望这可以帮助你...

6

Based on this Apple's documentation

UIViewController *viewControllerToBeShown=[[UIViewController alloc]init];

//viewControllerToBeShown.view.frame = self.view.frame;

viewControllerToBeShown.view.backgroundColor = [UIColor orangeColor];



AppDelegateClass *yourAppDelegate  =(AppDelegateClass*)[UIApplication sharedApplication].delegate;


UIView *myView1 = yourAppDelegate.window.rootViewController.view;

UIView *myView2 = viewControllerToBeShown.view;

myView2.frame = yourAppDelegate.window.bounds;


[yourAppDelegate.window addSubview:myView2];


CATransition* transition = [CATransition animation];
transition.startProgress = 0;
transition.endProgress = 1.0;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
transition.duration = 5.0;

// Add the transition animation to both layers
[myView1.layer addAnimation:transition forKey:@"transition"];
[myView2.layer addAnimation:transition forKey:@"transition"];

myView1.hidden = YES;
myView2.hidden = NO;


yourAppDelegate.window.rootViewController = viewControllerToBeShown;

Swift

        guard let appDelegate = UIApplication.shared.delegate,
            let appDelegateWindow = appDelegate.window,
            let appDelegateView = window.rootViewController?.view,
            let viewContollersView = viewController.view else {
            return
        }
        viewContollersView.frame = (appDelegateWindow?.bounds)!
        appDelegate.window??.addSubview(viewContollersView)
        let transition = CATransition()
        transition.startProgress = 0
        transition.endProgress = 1.0
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromRight
        transition.duration = 0.35
        appDelegateView.layer.add(transition, forKey: "transition")
        viewContollersView.layer.add(transition, forKey: "transition")
        appDelegateView.isHidden = true
        viewContollersView.isHidden = false
        appDelegateWindow?.rootViewController = viewController

1
这个可以运行,但是有没有办法让之前的视图控制器不变黑?当我执行代码的视图控制器一旦动画开始就会变成黑色。 - SleepNot
在Swift中,请检查这个答案...它有效。 https://dev59.com/jYDba4cB1Zd3GeqPJ-F8#35224877 - Hardik Darji
这看起来不像UINavigationController中的转场,你必须使用一个假的vc。 - Kamen Dobrev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接