Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UIView slide transition

gplumbgplumb Posts: 3New Users
edited March 2012 in iPhone SDK Development
Hi guys (and girls! :p)

I was wondering how I could achieve a slide left/right with a UIView?
I got the standard flip transition working using the following code:

// Flippin' marvellous!
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache:YES];

[[mainScreenController view] removeFromSuperview];
[window addSubview:[myNextScreenController view]];
[UIView commitAnimations];


I was wondering how would I modify this code to slide the second view in from right to left? (Sorry, I am a n00b to ObjC/iPhone atm!)

Thanks
Gee
Post edited by gplumb on

Replies

  • HackHack San FranciscoPosts: 2,067Registered Users @ @ @ @
    edited March 2009
    // get the view that's currently showing
    	UIView *currentView = self.view;
    	// get the the underlying UIWindow, or the view containing the current view view
    	UIView *theWindow = [currentView superview];
    	
    	// remove the current view and replace with myView1
    	[currentView removeFromSuperview];
    	//[theWindow addSubview:newView];
    	
    	// set up an animation for the transition between the views
    	CATransition *animation = [CATransition animation];
    	[animation setDuration:0.5];
    	[animation setType:kCATransitionPush];
    	[animation setSubtype:kCATransitionFromLeft];
    	[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    	
    	[[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
    
  • gplumbgplumb Posts: 3New Users
    edited March 2009
    Hi Bertrand21

    Thank you for your help - this code was exactly what I was after. I removed the first couple of lines as I already had these set up as properties ;)

    Just a quick note to anyone else who is also new to this, you will need to import the QuartzCore framework and add the appropriate #import line at the top of your code.

    Thanks ;)
    Gee
  • Duncster2k4Duncster2k4 Posts: 33Registered Users
    edited April 2009
    gplumb wrote: »
    Hi Bertrand21

    Thank you for your help - this code was exactly what I was after. I removed the first couple of lines as I already had these set up as properties ;)

    Just a quick note to anyone else who is also new to this, you will need to import the QuartzCore framework and add the appropriate #import line at the top of your code.

    Thanks ;)
    Gee


    Thanks for this! Worked perfectly :)

    D.
  • mihirsmmihirsm Posts: 3New Users
    edited July 2009
    Hi,

    I followed the above code and the view slides nicely. Though the new view never shows up.

    In the "SwitchToView1" method I do the following:

    [self.view addSubview:newView]

    Plz, can someone let me know what I am missing.

    Thanks.
  • asUwishasUwish Posts: 10Registered Users
    edited October 2009
    Hi there,

    I'm trying to slide a view out and a new one in but the code isn't working. did you get this to work?

    asUwish.
    mihirsm wrote: »
    Hi,

    I followed the above code and the view slides nicely. Though the new view never shows up.

    In the "SwitchToView1" method I do the following:

    [self.view addSubview:newView]

    Plz, can someone let me know what I am missing.

    Thanks.
  • erastusnjukierastusnjuki Posts: 1New Users
    edited December 2009
    Just for those who are stranded like I was ...here's some code that worked for me. with this the new view slid in ;)
    //1. Add the QuatzCore Framework from library to the Frameworks folder
    //2. At the top include the header file

    #import <QuartzCore/QuartzCore.h>

    //3. Implement the code

    yourViewController = [[YourViewController alloc]
    initWithNibName:@"YourViewController"
    bundle:nil];
    // get the view that's currently showing
    UIView *currentView = self.view;
    // get the the underlying UIWindow, or the view containing the current view
    UIView *theWindow = [currentView superview];

    UIView *newView = yourViewController.view;

    // remove the current view and replace with myView1
    [currentView removeFromSuperview];
    [theWindow addSubview:newView];

    // set up an animation for the transition between the views
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.5];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromRight];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

    [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
  • darkplasticdarkplastic Posts: 2New Users
    edited April 2010
    Just for those who are stranded like I was ...here's some code that worked for me. with this the new view slid in ;)
    //1. Add the QuatzCore Framework from library to the Frameworks folder
    //2. At the top include the header file

    #import <QuartzCore/QuartzCore.h>

    //3. Implement the code

    yourViewController = [[YourViewController alloc]
    initWithNibName:@"YourViewController"
    bundle:nil];
    // get the view that's currently showing
    UIView *currentView = self.view;
    // get the the underlying UIWindow, or the view containing the current view
    UIView *theWindow = [currentView superview];

    UIView *newView = yourViewController.view;

    // remove the current view and replace with myView1
    [currentView removeFromSuperview];
    [theWindow addSubview:newView];

    // set up an animation for the transition between the views
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.5];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromRight];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

    [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];

    Hey guys, all this seems to work, just doesn't seem to create a valid reference to the new viewController.

    After the transition any (IBAction) calls from the new view seem to crash the application...am I missing something here?
  • darkplasticdarkplastic Posts: 2New Users
    edited April 2010
    Hey guys, all this seems to work, just doesn't seem to create a valid reference to the new viewController.

    After the transition any (IBAction) calls from the new view seem to crash the application...am I missing something here?

    *EDIT*

    Managed to get it to work, but this method seems to have a bunch of issues when it comes to orientation...

    Wander if there is a way to get it to closer emulate presentModalViewController behaviour (i.e new view is animated with correct orientation and size)
  • chrigilchrigil Posts: 19Registered Users
    edited August 2010
    *EDIT*

    Managed to get it to work, but this method seems to have a bunch of issues when it comes to orientation...

    Wander if there is a way to get it to closer emulate presentModalViewController behaviour (i.e new view is animated with correct orientation and size)

    How did you manage to work around this problem darkplastic? I'm struggling with making IBAction calls too.

    Any advice is appreciated.

    Thanks

    Chris
  • agfa555agfa555 Posts: 3New Users
    edited October 2010
    THANK YOU!!!!!!
    Just for those who are stranded like I was ...here's some code that worked for me. with this the new view slid in ;)
    //1. Add the QuatzCore Framework from library to the Frameworks folder
    //2. At the top include the header file

    #import <QuartzCore/QuartzCore.h>

    //3. Implement the code

    yourViewController = [[YourViewController alloc]
    initWithNibName:@"YourViewController"
    bundle:nil];
    // get the view that's currently showing
    UIView *currentView = self.view;
    // get the the underlying UIWindow, or the view containing the current view
    UIView *theWindow = [currentView superview];

    UIView *newView = yourViewController.view;

    // remove the current view and replace with myView1
    [currentView removeFromSuperview];
    [theWindow addSubview:newView];

    // set up an animation for the transition between the views
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.5];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromRight];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

    [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
  • aoberoiaoberoi Posts: 11Registered Users
    edited November 2010
    Bertrand21 wrote: »
    // get the view that's currently showing
    	UIView *currentView = self.view;
    	// get the the underlying UIWindow, or the view containing the current view view
    	UIView *theWindow = [currentView superview];
    	
    	// remove the current view and replace with myView1
    	[currentView removeFromSuperview];
    	//[theWindow addSubview:newView];
    	
    	// set up an animation for the transition between the views
    	CATransition *animation = [CATransition animation];
    	[animation setDuration:0.5];
    	[animation setType:kCATransitionPush];
    	[animation setSubtype:kCATransitionFromLeft];
    	[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    	
    	[[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
    

    I have a couple questions about memory management using this code.

    Assuming this is a method inside a UIViewController, when does this object get released? Its view isn't in the window anymore so it should be save to release right?

    Who is retaining the newView's view controller? Where is it alloc-init-ed? If I want to initialized it from a nib, then load the view this way, will the view controller stick around beyond the end of this method?
  • HakimoHakimo Posts: 12Registered Users
    edited May 2011
    Thanks very much for the code. I have some questions though. If I'm adding a second view, I noticed after going to the second view and going back to the root view, the screen goes white. I can only see it after deleting the line "[currentView removeFromSuperview];".

    What I like to know, since the mainview is the rootview, is it necessary to remove it from view. How does this effect the memory?

    And if I have to remove it from the root view, should I insert the same code on the 2nd view? As in adding a new subview? How would this effect the performance?

    Hope this makes sense.
    Just for those who are stranded like I was ...here's some code that worked for me. with this the new view slid in ;)
    //1. Add the QuatzCore Framework from library to the Frameworks folder
    //2. At the top include the header file

    #import <QuartzCore/QuartzCore.h>

    //3. Implement the code

    yourViewController = [[YourViewController alloc]
    initWithNibName:@"YourViewController"
    bundle:nil];
    // get the view that's currently showing
    UIView *currentView = self.view;
    // get the the underlying UIWindow, or the view containing the current view
    UIView *theWindow = [currentView superview];

    UIView *newView = yourViewController.view;

    // remove the current view and replace with myView1
    [currentView removeFromSuperview];
    [theWindow addSubview:newView];

    // set up an animation for the transition between the views
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.5];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromRight];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

    [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
    <b><a href="http://www.hakimo-studio.com" target="_blank">www.hakimo-studio.com</a></b><br />
    <b><a href="http://www.lcgdi.com" target="_blank">www.lcgdi.com</a></b><br />
    "Learn and Play"<br />
    <a href="http://www.facebook.com/pages/Shah-Ala
  • dynamite88dynamite88 Posts: 10Registered Users
    edited January 2012
    Hey guys, all this seems to work, just doesn't seem to create a valid reference to the new viewController.

    After the transition any (IBAction) calls from the new view seem to crash the application...am I missing something here?


    Hi there.. this is an old entry, but was hoping you can share your solution to the invalid references in the new viewcontroller.
    <ul><li><a href="http://itunes.apple.com/ca/app/citizenship-calculator-for/id493866541?mt=8" target="_blank">Citizenship Calculator for Canadian Permanent Residents</a></li>
    <li><a href="http://itunes.apple.com/ca/app/citizenship-flash-cards-discover/id50
  • Duncan CDuncan C Posts: 9,019Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited January 2012
    dynamite88 wrote: »
    Hi there.. this is an old entry, but was hoping you can share your solution to the invalid references in the new viewcontroller.

    This whole thread discusses something you should not do: hosting one view controller's view inside another view controller. The system is built around the idea that a view controller manages the entire screen.

    The good news is that with iOS 5, it is now possible (easy, even) to do exactly what everybody here is trying to do.

    You make one view controller the parent view controller. It adds all the view controllers that will display content inside it as child view controllers using the new addChildViewController method.

    Then install a view controller's view as a subview of the parent view controller. When you're ready to swap it, use the new method transitionFromViewController:toViewController:duration:options:animations:completion:. That method includes several transtions "for free", and also takes an animation block that makes it trivial to do slides, cross-fades, and other transitions that change animatable view properties.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • CocoaNoobCocoaNoob Posts: 1New Users
    edited March 2012
    Duncan C wrote: »
    This whole thread discusses something you should not do: hosting one view controller's view inside another view controller. The system is built around the idea that a view controller manages the entire screen.

    The good news is that with iOS 5, it is now possible (easy, even) to do exactly what everybody here is trying to do.

    You make one view controller the parent view controller. It adds all the view controllers that will display content inside it as child view controllers using the new addChildViewController method.

    Then install a view controller's view as a subview of the parent view controller. When you're ready to swap it, use the new method transitionFromViewController:toViewController:duration:options:animations:completion:. That method includes several transtions "for free", and also takes an animation block that makes it trivial to do slides, cross-fades, and other transitions that change animatable view properties.

    Duncan,

    Hello! I know this post is very old so hopefully you will receive an alert from the site regarding my question to you. That question being the following:

    I'm using the method transitionFromViewController:toViewController in a UIViewController <UITabBarDelegate> class, lets call this MyController. What I'm doing is in the tabBar delegate method tabBar:didSelectItem I'm detecting which tab is selected by the user. Then i'm reloading a tableView. The first 3 of the 5 tabs in this tabBar just display different data in the tableView. This all works perfectly however I would like to add a transition and/or some animation when a different tab is selected. I'm doing something wrong in this code, I think its because I'm using self.view for both the fromView and toView portions of the method... Code is below:
        // code above snipped to keep this short...
        else if(selectedTabTagId_ == 2)
        {
            tabMode_ = [NSString stringWithString:@"__DISTANCE__"];
            [myArray_ removeAllObjects];
            myArray_ = [myDatabase_ fetchUsingQueryType:tabMode_];
            
            UIView *fromView = self.view;
            UIView *toView = self.view;
            
            [UIView transitionFromView:fromView toView:toView duration:1.5 options:UIViewAnimationOptionTransitionCurlDown completion:^(BOOL finished)
             {
                 if(finished)
                 {
                     self.title = @"Sorted By Distance";
                     [tableView_ reloadData];
                 }
             }];
        }
        else if(selectedTabTagId_ == 3)
        {
                // code below snipped to keep this short...
    

    The transition takes place however the screen starts with my tableView_ displayed and turns to a black screen with nothing displayed.

    Now - I can get a subset of the view to work if I do the following. First some info on how the view is setup.

    I have a TableViewController named for example MyController.
    MyController
        Contains a View
            Contains a TableView
                Contains custom TableViewCells
            Contains a Tab Bar
                Contains 5 TabBar Items (tabs)
        Contains a Navigation Bar
            Contains a left and right bar button.
    

    MyController is defined like so:
    .h file
        @interface MyController : UIViewController <UITabBarDelegate, UITableViewDelegate, UITableViewDataSource, MKMapViewDelegate, UIActionSheetDelegate>
    
        @property (strong, nonatomic) IBOutlet UITableView *tableView;
        @property (strong, nonatomic) IBOutlet UITabBar *tabBar;
        @property (strong, nonatomic) IBOutlet UIView *myControllerView;
        // and a bunch of other stuff that is snipped out for this example
        @end
    

    If in the .m file I replace
        UIView *toView = self.view;
    

    with
        UIView *toView = tableView_;
    

    It works, however the tabBar_ disappears. Not what I want because now the user is stuck on this page of the app.

    If I try to use the View (myControllerView_ - which contains both the tableView_ and the tabBar_), I get the same broken results - a transition to a black screen.
        UIView *toView = myControllerView_;
    

    So, any Idea on what I'm doing wrong? Perhaps I'm going about this the wrong way?

    Again, I would like the tableView_ to refresh when the [tableView_ reloadData] method is called but with an animation, and the tabBar_ must always be on the bottom of the View.

    Thanks in advance - hope you see this!
Sign In or Register to comment.