Giter VIP home page Giter VIP logo

tsanimatedrootviewswitcheroo's Introduction

TSAnimatedRootViewSwitcheroo

TSAnimatedRootViewSwitcheroo is a simple container controller for your UIWindow's rootViewController that lets you transition from one root controller to another.

TSAnimatedRootViewSwitcheroo helps you manage your app's root controllers. It's very common to have several slices in your application. For example:

  • Normal day-to-day app use (UITabBarController with UINavigationControllers on each tab)
  • Intro (UIPageViewController)
  • Logged out (perhaps UINavigationController with a title)
  • Upgrading DB (plain UIViewController)
  • Network down (another plain UIViewController)

TSAnimatedRootViewSwitcheroo helps your app transition smoothly between these very different structures using the iOS 7 UIViewControllerAnimatedTransitioning Protocol, including animation libraries like VCTransitionsLibrary.

Example

First, grab it with Cocoapods:

pod 'TSAnimatedRootViewSwitcheroo', '~> 1.0.0'

Now let's assume we have a singleton class which creates our separate root controller, with a simple interface like this:

@interface AppRoots : NSObject

// Normal use
+ (UITabBarController *)appRoot;

// Other uses
+ (UIPageViewController *)introRoot;
+ (UINavigationController *)loggedOutRoot;
+ (UIViewController *)upgradingDBRoot;
+ (UIViewController *)networkDownRoot;

@end

Then we can easily add our TSAnimatedRootViewSwitcheroo to help transition from one to another.

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIViewController *root = nil;

    if (/* logged in */) {
        root = [AppRoots appRoot];
    } else {
        root = [AppRoots introRoot];
    }

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [TSAnimatedRootViewSwitcheroo switcherooWithRoot:root andDelegate:self];
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)signOut {
  // Do some stuff
  [TSAnimatedRootViewSwitcheroo useRoot:[AppRoots loggedOutRoot] direction:TSSwitcherooAnimationDirectionReverse];
}

- (void)signIn {
  // Do some stuff
  [TSAnimatedRootViewSwitcheroo useRoot:[AppRoots appRoot] direction:TSSwitcherooAnimationDirectionForward];
}

- (id<UIViewControllerAnimatedTransitioning>)switcheroo:(TSAnimatedRootViewSwitcheroo *)switcheroo
                               animationControllerForDirection:(TSSwitcherooAnimationDirection)direction
                                            fromViewController:(UIViewController *)fromViewController
                                              toViewController:(UIViewController *)toViewController {

    // Let's use some awesome transitions from the VCTransitionsLibrary
    CEReversibleAnimationController *animator = [CENatGeoAnimationController new];
    animator.reverse = (direction == TSSwitcherooAnimationDirectionReverse);
    return animator;

    // If we want a Cube animation for login/logout, but a Flip animation for UpgradeDB => app
    // then we'd check the fromViewController/toViewController pairs and return the right animation.
    
    // Return nil for no animation.
}

@end

The main thing is that the TSAnimatedRootViewSwitcheroo is used as your window's rootViewController, and then you can change the root controller as needed by calling + useRoot:direction:.

Lastly, you have full control over which animation is used to transition between your stacks, including custom animations you create.

Because this is a UIViewController that implements the custom container controller methods, you can do quite a bit more with it. The code's evolution is will always prefer to stick with switching between root controllers as the primary use case.

tsanimatedrootviewswitcheroo's People

Contributors

timshadel avatar mcmurrym avatar

Watchers

Edward Patel avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.