Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removeFromSuperview with animation and views management

I did some digging around about this, but nothing really seems to answer my particular question (not even this: Is it possbile to removeFromSuperview with Animation?).

Basically, my app starts with a welcome screen, where a user clicks on "sign in", then goes to the sign in view, then getting to a tab bar view, which is the actual app.

The way I did it, is that I wrote a custom class - TabBarController, which sets up all the tabs and their respective view controllers. Now, when the user clicks on "sign in" i am calling removeFromSuperview and present the tabbar.

I am trying to find a way to animate the transition from the sign in page to the tab bar. I tried some proposed solutions around here, but none seems to do the job. Here is my code in the signin.m view controller. I am looking to animate out the current view (ideally, not just by fading out, but more cool stuff like flips, etc.).

//when done signing in --> go to the tab bar view 
-(IBAction)done:(id)sender {

TabBarController  *tabController = [[TabBarController alloc] init];
[UIView beginAnimations:@"removeWithEffect" context:nil];
[UIView setAnimationDuration:4.0];
self.parentViewController.view.frame = CGRectMake(0,0,320,480);
self.parentViewController.view.alpha = 1.0f;
[UIView commitAnimations];
[self.parentViewController.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2.5f];
[self presentModalViewController:tabController animated:YES];

}

Appreciate any help!

like image 845
TommyG Avatar asked Dec 17 '22 11:12

TommyG


2 Answers

That can't work that way. presentModalViewController dislpays the view of a viewController over the own view. It won't replace the source viewController (self). Since you remove self.parentViewController.view from the view hierarchy, it can't present your tabController modally because you have removed self.

Anyway, i would recommend you another way to achieve your view layout: Create a tabBarViewController and add its view to a rootView (self.window in the app delegate or whatever you are using now). Then add your login-view to the same view. Due the view hierarchy, the login-view will be displayed above the tabBar.view. And the done button should be implemented this way: (i'm using block syntax for animation as it should be)

-(IBAction)done:(id)sender {
    [UIView animateWithDuration:1.0 
                     animations:^{
                         self.view.frame = CGRectMake(0, 480, 320, 480);
                         self.view.alpha = 0.0
                     } 
                     completion:^(BOOL finished){
                         [self.view removeFromSuperView];
                     }
     ];
}

You can animate more things than just the alpha, size or position. Just take a look about animations in the documentation. I guess, you'll be interested in view.transform to commit flip animations. ;)

like image 111
yinkou Avatar answered Dec 26 '22 10:12

yinkou


This is how you have to remove the view after animating it.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelay:2.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:myView];
[UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];

[UIView commitAnimations];

Hope this helps. Happy coding.

like image 41
stack2012 Avatar answered Dec 26 '22 11:12

stack2012