Change RootViewcontroller with the Push Transition effect

前端 未结 3 1905
情话喂你
情话喂你 2021-02-09 20:40

in my iOS App i need to change the rootviewController of the window in between of app .so when i change my rootviewcontroller dyncamically its flicking the view before its chang

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-09 21:14

    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
    

提交回复
热议问题