ReSwift-Router icon indicating copy to clipboard operation
ReSwift-Router copied to clipboard

Animate transition between view controllers

Open ranhsd opened this issue 8 years ago • 1 comments

Hi, I am currently trying to refctor my app from MVVM to ReSwift. One of the things that i adopt is the ReSwiftRouter. The router allows me to navigate from one screen to another (either by push view controller into a navigation controller or by present it modally) .

I also consider to use Hero for animate transition between view controllers. Hero created some extension to UIViewController which allows you to send a view controller there and animationType and then it will automatically animate the transition between both view controllers. Now I wanted to know where is the best place to add this transition code? What i did now is to add it to the routable via the following lines of code:

` Hero.shared.setDefaultAnimationForNextTransition(.zoom) Hero.shared.setContainerColorForNextTransition(.lightGray)

    let navigationController = UINavigationController.init(rootViewController: viewController)
    self.viewController.hero_replaceViewController(with: navigationController)
    
    return SignUpRoute(viewController)`

I am not sure that this is the best place or not to put the code.

What i was thinking is maybe to write an extension to Routable that will receive the source view controller and the destination view controller and animation type and this function will execute the animation.

Wanted to know what do you think or maybe there is a better solution?

Thanks.

ranhsd avatar Mar 09 '17 09:03 ranhsd

I was able to leverage Hero for this by presenting my viewcontrollers using modal presentation. Notice that I'm setting hero transition animations on both push and pop, which allows to me to have symmetry. For some reason, using Hero's navigationAnimationType wasn't working, which is why I chose modal presentation, otherwise I would've stuck to show.

class MainRoutable: Routable {
    
    let viewController: UIViewController
    
    init(_ viewController: UIViewController) {
        self.viewController = viewController
    }
    
    func pushRouteSegment(
        _ routeElementIdentifier: RouteElementIdentifier,
        animated: Bool,
        completionHandler: @escaping RoutingCompletionHandler) -> Routable {
        if routeElementIdentifier == "UsersRoute" {
            let userViewController = storyboard.instantiateViewController(withIdentifier: "UsersViewController")
            userViewController.hero.modalAnimationType = .zoom
            (self.viewController as! UINavigationController)
                .childViewControllers
                .first?
                .present(userViewController, animated: true, completion: completionHandler)
            
            return UsersRoutable()
        }
        
        fatalError("Cannot handle this route change!")
    }
    
    func popRouteSegment(
        _ routeElementIdentifier: RouteElementIdentifier,
        animated: Bool,
        completionHandler: @escaping RoutingCompletionHandler) {
        print("hello")
        if routeElementIdentifier == "UsersViewController" {
            (self.viewController as! UINavigationController).visibleViewController?.hero.modalAnimationType = .zoomOut
        }
        self.viewController.dismiss(animated: true, completion: completionHandler)
    }
}

nitindhar7 avatar Feb 19 '18 22:02 nitindhar7