ios - 使用 Push Transition 效果更改 RootViewcontroller

标签 ios iphone objective-c uiviewanimationtransition

在我的 iOS 应用程序中,我需要在应用程序之间更改窗口的 rootviewController。因此,当我动态更改我的 rootviewcontroller 时,它会在更改之前轻弹 View 。但我想要的是在更改 rootviewcontroller 时提供平滑过渡.

我尝试过以下但不是我想要的过渡。

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

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

我想要特定的转换,例如导航 Controller pushview 转换。

任何人都可以告诉我如何实现这一目标吗?

最佳答案

根据 Hardik Darji 的回答,我创建了 UIWindow 扩展以将 rootViewController 与模拟系统动画的可配置动画类型交换 - 推送、弹出、呈现和关闭。

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()
    }
}

关于ios - 使用 Push Transition 效果更改 RootViewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24362957/

相关文章:

iphone - UIActionSheet 需要很长时间才能响应

iphone - 在iOS中加快精简版TTS

objective-c - 何时分配/初始化一个 ivar,何时不分配/初始化

objective-c - 用于维护磁盘缓存的库

android - 介于IOS和安卓之间的谷歌

ios - 从终端更改 Expo 中的配置文件

ios - iOS Siri工具包集成-调用应用程序并发送自定义指令

iphone - 在 Objective-C 中使用数组试图简化大量重复代码

ios - 使用Swift在IOS开发中导航栏覆盖音乐播放器

ios - NSDictionary加载18个键值对的信息总是读为nil而不是