4

I'm using ECSliding and I have this problem!

In my project there are this files:

InitViewController (ECSlidingController)

FirstViewController (UIViewController)

SecondViewController (UIViewController)

LeftMenuViewController (UIViewController)

I use InitView to instatiate my FirstView as the topview.

In my FirstView there is a button, when pressed sets SecondView as topview.

Is it possible to animate the changing topview like I'm opening a new view? or how can I open a new view that uses ECSliding like the first one?

I'm using this code to change topview:

self.slidingViewController.topViewController = second;
[self.slidingViewController resetTopView];

The animation that I want could just be the default one, like:

[self presentViewController:(*UIViewController) animated:YES completion:nil];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Tortuga
  • 97
  • 3
  • 10

5 Answers5

3

Subclass ECSlidingViewController. I call my subclass DeckController:

In DeckController.h:

- (void)setTopViewController:(UIViewController *)topViewController withTransition:(CATransition*)transition;

In DeckController.m:

- (void)setTopViewController:(UIViewController *)topViewController withTransition:(CATransition*)transition {

    self.topViewController = topViewController;
    [UIView animateWithDuration:0.25f animations:^() {
        [[self.view layer] addAnimation:transition forKey:@"topViewTransition"];
    }];
}

Then in the view controller that wants to switch to the new top view, do something like this:

CATransition *animation = [CATransition animation];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation setFillMode:kCAFillModeBoth];
[animation setDuration:.25];

DeckController *deck = (DeckController*)self.slidingViewController;
[deck setTopViewController:newTopViewController withTransition:animation];
Alex Nauda
  • 4,126
  • 1
  • 24
  • 24
0

Probably your best option is to use the animation methods of the sliding view controller. Animate the top view off screen, in the completion block change the top view controller (to second) then start a new animation to animate the top view back onto screen.

[self.slidingViewController anchorTopViewOffScreenTo:...
                                          animations:...
                                          onComplete:^{
    self.slidingViewController.topViewController = second;
    [self.slidingViewController resetTopView];
}];
Wain
  • 118,658
  • 15
  • 128
  • 151
  • How can I do that? Can you gently write some generic code to help me understand? – Tortuga May 20 '13 at 22:40
  • I tried, but nothing! The view disappear and remain my left or right menu depending on which (ECSide) I used. Could you be so gentle to write specific code for my case? I would really appreciate! – Tortuga May 21 '13 at 22:53
  • I Already did like you wrote and "The view disappear and remain my left or right menu depending on which (ECSide) I used." I added: `[self.slidingViewController anchorTopViewOffScreenTo:... animations:... onComplete:^{ self.slidingViewController.topViewController = second; [self.slidingViewController resetTopView]; }]; [self.slidingViewController anchorTopViewTo:... animations:nil onComplete:nil];` – Tortuga May 21 '13 at 23:20
  • My topview is there now visible, but anchored on left or right. not fully open. – Tortuga May 21 '13 at 23:23
  • Read through the other available methods and try some of them. – Wain May 22 '13 at 06:31
0

I just use same operation as when Im clicking on a menuitem in my left menu.

[self.anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
        CGRect frame = self.topViewController.view.frame;
        self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"secondVC"];
        self.topViewController.view.frame = frame;
        [self resetTopView];
    }];

Just generalize this, and you can call it everywhere. It does the animation by itself. You can do as @Alex, but if you don't have to do some custom animation, this is all you need.

NB. in my example Im using this in the initViewController (for which is the ECSlidingViewController, therefore i'm sending to self), for forcing user to a specific viewController. If you need to change view in any subViewControllers you just sending to self.slidingViewController like the example below:

[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
        CGRect frame = self.slidingViewController.topViewController.view.frame;
        self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"secondVC"];
        self.slidingViewController.topViewController.view.frame = frame;
        [self.slidingViewController resetTopView];
    }];
Hulvej
  • 3,895
  • 1
  • 18
  • 21
0

You can manually create the animation then set the topViewController at the end so that the sliding view controller is in the state that you expect.

SecondTopViewController *secondTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondTop"];
secondTopViewController.view.frame = self.slidingViewController.view.bounds;
secondTopViewController.view.alpha = 0;

// temporarily add the second top view so that we can animate it in.
[self.slidingViewController.view addSubview:secondTopViewController.view];

[UIView animateWithDuration:0.5 animations:^{
    secondTopViewController.view.alpha = 1;
} completion:^(BOOL finished) {
    // remove the second top view.
    [secondTopViewController.view removeFromSuperview];

    // let sliding view controlller take over from here
    self.slidingViewController.topViewController = secondTopViewController;
}];

See my working example here: https://github.com/enriquez/ECSlidingViewController/tree/so-16658539

Michael Enriquez
  • 2,520
  • 21
  • 13
0

@Michael-Enriquez's answer converted to swift.

let viewController =  (self.storyboard?.instantiateViewControllerWithIdentifier("ViewController"))! as? ViewController
    let navController =  UINavigationController(rootViewController: viewController!)
    navController.view.frame =  self.slidingViewController().view.bounds
    navController.view.alpha = 0
    self.slidingViewController().view.addSubview(navController.view)
    UIView.animateWithDuration(0.25, animations: {
        navController.view.alpha = 1
        }, completion: {(finished:Bool) -> Void in
            navController.view.removeFromSuperview()
            self.slidingViewController().topViewController =  navController
    })
anuraagdjain
  • 86
  • 2
  • 7