Change RootViewcontroller with the Push Transition effect

前端 未结 3 1885
情话喂你
情话喂你 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:10

    Here is code in Swift for how to make Push and Pop animation in 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)
    

    Hope this helps...

提交回复
热议问题