UITabBarController View Transition error - iphone

OKay I have been working on this problem for a while now, And It is well beyond my expertise thus why I am asking for help again.
I am trying to animate the transition for a tabbarbutton click to another view. I have declared in both viewController (ViewWillAppear) methods the code below
- (void)viewWillAppear:(BOOL)animated
{
//TODO: Fix transition animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
[super viewWillAppear:animated];
}
FirstViewController to the right and SecondViewController to the left.
Now the problem is happening when the user loads the app for the first time, once everything loads up and the user clicks on the tabbarbutton to go to the second view, it dose not flip.. but when you go back to the FirstView it animates then if you go to the second again it will animate this time round.. Dose anyone have any idea why this is happeneing? if you do your help would be greatly appreciated!
UPDATE::
I am trying to add a animation into viewDidLoad, however there is already an animation for an opening sequence I am loading straight away.
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//-- Start Opening Animation ------->
CGRect vaultTopFrame = vaultTop.frame;
vaultTopFrame.origin.y = -vaultTopFrame.size.height;
CGRect vaultBottomFrame = vaultBottom.frame;
vaultBottomFrame.origin.y = self.view.bounds.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.5];
[UIView setAnimationDelay:2.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
vaultTop.frame = vaultTopFrame;
vaultBottom.frame = vaultBottomFrame;
[self.view bringSubviewToFront:vaultTop]; //this should place this subview above any others on the same page
[self.view bringSubviewToFront:vaultBottom]; //this should place this subview above any others on the same page
[UIView commitAnimations];
I think this might be messing things up for me what do you think?

You do not have animation when the app loads up. Set animation in viewDidLoad for animation when the view loads up and viewDidAppear will give you animations when the user makes transition after wards (every time the tab is clicked).

This may or may not be the problem, but you should call
[super viewWillAppear:animated];
at the beginning of the method, before your animation code.

Related

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!
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. ;)
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.

trying to remove a subview

I have 2 subviews on the stage (a splash screen, and the main screen) once the splash screen finishes playing its audio it calls a function called -(void)audioComplete which is suppose to fade out the splash screen, revealing the main screen. I can't seem to get that working.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:splashController.view];
[self.window addSubview:rootController.view];
[self.window makeKeyAndVisible];
return YES;
}
-(void)audioComplete{
NSLog(#"REMOVE FROM STAGE");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:splashController.view];
splashController.view.alpha = 0.0;
[UIView commitAnimations];
[splashController release];
}
If I add NSLog(#"%#",[splashController.view superview]); in the audioComplete function I get (null), but not when I add it to the didFinishLaunchingWithOptions function.
Apple highly recommends against using splash screens. Instead, you should use an image called 'Default.png' in the root directory. This will get displayed while the application is launching and make it appear that your application is faster than it actually is. Apple could potentially reject your submission to the app store if you create your own loading screen.
Could be one of two things:
1) Seems like your rootControllerView is being added on top of the splashControllerView.
Maybe your animation is happening but you can't see it as your rootControllerView is blocking it.
Try reversing your order of addSubview.
2) Don't release your splash view in the same method you're using for animation. Wait for the animation to finish before releasing your view. You can do this by:
-(void)audioComplete
{
NSLog(#"REMOVE FROM STAGE");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidFinishSelector:#selector(releaseView)];
splashController.view.alpha = 0.0;
[UIView commitAnimations];
}
-(void)releaseView
{
[splashController release];
}
Also, as a best practice for memory management, don't release splashControllerView directly.
What I do is release a view immediately after adding it as a subview (adding a subview increases it's retain count).
When I'm done with the subview, I simply call [subView removeFromSuperView] which reduces the retain count and makes it zero.
Simply put:
UIView *view = [[UIView alloc] init]; //retain count = 1
[self.view addSubview:view]; //retain count = 2
[view release]; //retain count = 1
//do stuff with the view
[view removeFromSuperview]; //retain count = 0;
I don't really see your question, because in your didFinishLaunchingWithOptions you're adding your view and it's really there! So you will receive an answer every time you call NSLog(#"%#",[splashController.view superview]); but in audioComplete you release your subView and it's away... After this you can't get access to the superview, because - you're subView is released...
I would use this:
-(void)audioComplete{
NSLog(#"REMOVE FROM STAGE");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:splashController.view];
splashController.view.alpha = 0.0;
[UIView commitAnimations];
[splashController.view removeFromSuperview];
}
You'll get the same thing again, but you're subView is still there if you need something later.
For Fade in and Fade out images between splash and mainscreen. i suggest you could trying having two image in rootviewcontroller. made the fade in and fadeout animation between two UIIMageViews......
for fade in read out this sample tuts:
http://iosdevelopertips.com/user-interface/fade-transition-fade-images-in-and-out.html
during the animation, u can complete your audio streams

How to properly release a view after a UIView Animation ? :)

I have a method like the following:
- (void)add:(id)sender {
MyAddViewController *controller = nil;
controller = [[MyAddViewController alloc] initWithAddType:1];
//controller.delegate = self;
// Do some animation. Slide it in from the right side of the screen.
CGPoint initialCenter = self.view.center;
controller.view.center =
CGPointMake(initialCenter.x + 320, initialCenter.y);
// Add the subview now with it's center + 320 off the screen.
[self.view addSubview:controller.view];
[UIView beginAnimations:#"animation" context:NULL];
[UIView setAnimationDuration:0.35];
[UIView setAnimationDelegate:self];
controller.view.center = CGPointMake(initialCenter.x, initialCenter.y);
//[UIView setAnimationDidStopSelector:#selector(aMethodToBeCalledAfterAnimationEnd:finished:context:)];
[UIView commitAnimations];
//[controller release];
}
You see I have a controller release as the last line in my add method. If I uncomment this line the animation completely dies and it just dumps the view into place with no animation.
Is there a better way to do release without having to create another method which does cleanup via [UIView setAnimationDidStopSelect:#selector(aMethodToBeCalledAfterAnmiationEnd... ?
Or can I do something like setAnimationDidStopSelector:#selector([controller release]) ? :)
Thanks in advance!
Using setAnimationDidStopSelector: is the proper way to solve the generic problem of releasing resources after an animation completes.
Take a step back and reconsider what you're doing here, though. Why are you looking to free the controller you just created? If you are only creating the controller for the purpose of getting at the view, that isn't the right way to do it. Build yourself a factory method to create your view, or use the methods in NSBundle to load the view from a NIB.
You can do this:
[UIView setAnimationDelegate: controller];
[UIView setAnimationDidStopSelector:#selector(release)];
I see you tagged this iphone-sdk-4.0. You could use the new block-based animations to do this and any other cleanup. See the documentation for details.

UiView Transition inside UIView

this feels like a beginner question, but since I am not able to get it working in the intended-fashion, I am hoping somewhere out there is able to point me in the right direction.
What I am trying to achive is a transition of a view inside a view.
I fundamentally want to replace a view inside a view with another view(controller?!?) 's content.
lets say, I have an image of a book and I want to do a transition inside the book to another page (going from the calendar-view to the detailed daily view for example). I only want the "book-content"(white content area) to be included in that transition, and not the whole book itself(whole screen).
I can do a transition with the same view, no problem, but I dont get it working trying to replace the view with a completly different view.
It didnt matter having the views in seperate viewcontrollers, or all the views in one viewcontroller, so I were not able to get it working yet, but even worse, I am running out of ideas.
So HELP! If please somebody out there could be so kind and tell me what I am doing wrong.
Suggestions also very much appreciated.
Thanks!!
Tom
Maybe you could hide one view then show the other using a UIView Animations block.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view1.view cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view2.view cache:YES];
[UIView setAnimationDuration: 1.5];
[UIView commitAnimations];
view1.view.hidden = YES;
view2.view.hidden = NO;
Edit: Is this what you were looking for?
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[view1 viewWillDisappear:YES];
[view2 viewWillAppear:YES];
view1.view.hidden = YES;
view2.view.hidden = NO;
[view1 viewDidDisappear:YES];
[view2 viewDidAppear:YES];
[UIView commitAnimations];
Or, you could use something like this:
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[view1 removeFromSuperview];
[mySuperview addSubview:view2];
}
completion:nil];
Check out the exchangeSubviewsAtIndex: method in the UIView class. Just set up the 2 views that you want to transition from and to as subviews of another view; then call this method on that parent view.
Suppose you have a view controller with two views, A and B. They have identical frames so they occupy the same space in the controller's view, for your purposes displaying a page of a book. You want to display one and transition to the other.
One way would be to set the alpha property of one to 1.0 (opaque) and the other to 0.0 (transparent). Because this property can be animated you could do a fade from one to the other using a beginAnimations:context/commitAnimations, or use one of the block methods like the docs advise.
Another way would be to animate a change in frame in the two views. Set up the views so that one has a frame such that it is in the right place to display, the other has the same frame but with frame.origin.x equal to frame.size.width - it will be hidden because it is off-screen or hidden behind some other view. Animate the frame in by setting view A's frame.origin.x to -frame.size.width and frame B's frame.origin.x to the display position x origin. Then set (no animation) frame A's frame.origin.x to frame.origin.width again, update its content and you are ready to slide another page in from the right.

How to add an animation to the UIView in viewDidAppear?

I tried to add a animation to viewDidLoad and viewDidAppear, but it doesn't work:
- (void)viewDidAppear:(BOOL)animated{
[UIView beginAnimations:#"transition" context:NULL];
[UIView setAnimationTransition:110 forView:self.view cache:YES];
[UIView commitAnimations];
}
Why?
I had the same problem and I think I found the solution on this SO question.
When viewDidAppear gets called you still don't see anything on the screen (despite the name), but you are about to. You can then use a performSelector:withDelay or an NSTimer to launch your animation. The delay can just be 0.1 and your animation will play just when the screen appears.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"View did appear!");
[self performSelector:#selector(animationCode) withObject:nil afterDelay:0.1f];
}
- (void)animationCode {
// you animation code
}
You are not telling the view which state it should animate to so it won't do anything. You need to place code between beginAnimations:context: and commitAnimations that changes the appearance of the view (e.g. by removing one subview and adding another).
You're not using beginAnimations: and commitAnimations correctly. You're supposed to put something in between them that normally wouldn't be animated: e.g. with self.view.alpha = 0.5 you get a fading effect. They have no effect on anything that isn't between them.
By the time viewDidAppear: is called, your view, well... has appeared. It's too late to animate anything. What you actually want to do is something like this:
- (void)showMyViewWithAnimation {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationTransition:110 forView:childView cache:YES];
[parentView addSubview:childView];
[UIView commitAnimations];
}
In the example above, childView is what in your example is called self.view.
Please write out the name of the transition; no one knows what 110 is by looking at it. It's bad style. </pedantry>