views:

1082

answers:

3

Hi, i have a UIView with a UIToolBar with a button "Back" when i start a transition(UIViewAnimationTransitionFlipFromLeft) from a UiView to this view the button appears only at the end of the transition Why ? Pls Help me

Thanks

Code:

[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.90];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    UIViewController *coming = nil;
UIViewController *going = nil;
    UIViewAnimationTransition transition;
going = languageMenu;
coming = loadingmenu;
transition = UIViewAnimationTransitionFlipFromLeft;
   [UIView setAnimationTransition: transition forView:self.view cache:YES];

[coming viewWillAppear:YES];
[going viewWillDisappear:YES];

[going.view removeFromSuperview];
[self.view insertSubview: coming.view atIndex:0];
[going viewDidDisappear:YES];
[coming viewDidAppear:YES];

[UIView commitAnimations];
A: 

The way the cached transitions work is the iPhone takes a snapshot of the window and does some transforms on it as if it were an image. The uncached transitions actually redraw the live window as it is transformed.

Your problem seems to be that the back button isn't present in the view when the snapshot is taken. The solution may be to add the button manually rather than relying on the navigation view controller or something.

Dustin Voss
A: 

I'd say it depends on when you create the UIToolbar and its back button, and when you add them to the new view.

Also -- is there a particular reason why the viewWillAppear and viewWillDisappear lines need to be part of the animation? I'd experiment with pulling them out of the animation, and also moving the viewDidDisappear and viewDidAppear into a callback function invoked when the animation completes; see docs for UIView setAnimationDidStopSelector.

Not sure if that will help, but it might.

Amagrammer
Thanks Amagrammer i moved viewWillAppear and viewWillDisappear out of animation block and now work good :)Thank you!
You are very welcome! You are the first person I've really been able to help on Stack Overflow.
Amagrammer
A: 

It could be a problem with a difference between the two views. Have a look at both your views' attributes in the IB; did you specify a status bar for both? Or just one of them? This could cause a difference in vertical offset and can cause some problems I think.

You can perhaps solve this small problem by setting your two frames equal BEFORE the animation transition code:

newViewController.view.frame = self.view.frame;

(This should also then allow you to revert back to cache:YES)

On another matter, you may want to consider adding the subview onto the current window instead of the current window's current view, thus:

[[self.view superview] addSubview:newViewController.view];

This way you can remove the explicit calls to all the window events. You will also need to link the transition to your window instead of the current view otherwise animation will not work:

[UIView setAnimationTransition:transition forView:self.view.superview cache:YES];

I was struggling with similar problems, and eventually got this right. You may also want to try using QuartzCore foundation animations:

#import <QuartzCore/QuartzCore.h>

// ...

    // get the view that's currently showing
    UIView *currentView = self.view;
    [currentView retain];
    // get the the underlying UIWindow, or the view containing the current view
    UIView *theWindow = [currentView superview];

    UIView *newView = myNewViewController.view;
    newView.frame = currentView.frame;

    // add subview
    [theWindow addSubview:newView];

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

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

And to transition back, do the same (except in opposite direction of course) replacing your add subview with:

    [self.view removeFromSuperview];

With this the previous window will come to the foreground again, but its events will not be triggered (I'm still not sure why).

I hope this sorts things out for you and helps a lot of other people.

Marius