Advertise here




Advertise here

Howdy, Stranger!

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

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

UIView slide transition

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

  • Bertrand21Bertrand21 Posts: 2,009Registered Users
    // 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\"];
    Acquired.
  • gplumbgplumb Posts: 3New Users
    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
    gplumb;63796 said:
    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
    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
    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;109328 said:
    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
    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

    //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
    erastusnjuki;151083 said:
    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

    //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
    darkplastic;192314 said:
    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
    darkplastic;192358 said:
    *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
    THANK YOU!!!!!!
    erastusnjuki;151083 said:
    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

    //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
    Bertrand21;60768 said:
    // 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
    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.
    erastusnjuki;151083 said:
    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

    //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"];
    www.hakimo-studio.com

    www.lcgdi.com

    "Learn and Play"

    <a href="http://www.facebook.com/pages/Shah-Ala
  • dynamite88dynamite88 Posts: 10Registered Users
    darkplastic;192314 said:
    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.
  • Duncan CDuncan C Posts: 8,013Tutorial Authors, Registered Users
    dynamite88;403551 said:
    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.
  • CocoaNoobCocoaNoob Posts: 1New Users
    Duncan C;403573 said:
    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 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.