Изменить RootViewcontroller с помощью эффекта Push Transition

в моем приложении для iOS мне нужно изменить rootviewController окна между приложениями. Поэтому, когда я динамически меняю свой rootviewcontroller, он щелкает представление перед его изменением. Но я хочу обеспечить плавный переход при изменении rootviewcontroller.

я пробовал со следующим, но это не тот переход, который я хочу.

[UIView transitionWithView:self.window
                      duration:.8
                       options:UIViewAnimationOptionTransitionCurlUp
                    animations:^{

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

Я хочу конкретный переход, такой как переход pushview навигационного контроллера.

может ли кто-нибудь дать мне представление о том, как этого добиться?


person User 1531343    schedule 23.06.2014    source источник


Ответы (3)


На основе этой документации Apple

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;

Свифт

        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
person Vijay-Apple-Dev.blogspot.com    schedule 23.06.2014
comment
Это работает, однако есть ли способ, чтобы предыдущий контроллер просмотра не был черным? Контроллер представления, в котором я выполняю код, становится черным после запуска анимации. - person SleepNot; 10.07.2015
comment
В Swift проверьте этот ответ... его работа stackoverflow.com/a/35224877/1000906 - person Hardik Darji; 05.02.2016
comment
Это не выглядит как переход в UINavigationController, вы должны использовать поддельный vc - person Kamen Dobrev; 23.01.2017

Основываясь на ответе Хардика Дарджи, я создал расширение UIWindow для замены rootViewController на настраиваемый тип анимации, который имитирует системную анимацию — push, pop, Present и Disk.

Свифт 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()
}

}

person Wujo    schedule 18.04.2017
comment
Отлично, но для Swift 3 рекомендуется использовать нижний регистр для случаев в перечислении. - person Daniil Chuiko; 12.09.2017
comment
Я думаю, что нашел небольшую ошибку: viewWillAppear внутри вызова newViewController дважды - person Evansgelist; 06.05.2018
comment
Действительно есть небольшая ошибка, она дважды вызывает viewWillAppear и viewDidAppear - person thedp; 22.10.2018

Вот код на Swift, как сделать анимацию Push и Pop в rootviewcontroller.

//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)

Надеюсь это поможет...

person Hardik Darji    schedule 05.02.2016