iPhone Modal View Animation Help

2019-04-09 15:07发布

问题:

I have not done much animation, and I need some help with this. I have a tabBarController as my root controller, and I want to have another tabBarController, and I want to bring it up as a Modal View Controller, and I have a problem with the animation.

There are currently four animations for modalViewControllers, namely

typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;

I want a different animation - slide from the right to the left. - How can I do this animation?

Any help with this?


Edit:

My idea to push a tabBarController onto the navigation stack sucks! Apple's comment on this approach:

You never want to push a tab bar controller onto the navigation stack of a navigation controller. Doing so creates an unusual situation whereby the tab bar appears only while a specific view controller is at the top of the navigation stack. Tab bars are designed to be persistent, and so this transient approach can be confusing to users.

I am out of ideas. Someone help me with the animation for modal view controllers.

回答1:

You could write the animation code manually. Here are the general steps:

  • Create a subclass of UIViewController (essentially a dud controller to house your UITabBarController) - I usually call this ShellViewController.
  • In the ShellViewController's init method (whichever one you would use), set its frame outside of the screen to the right, e.g. [self.view setFrame:CGRectMake(320, 0, 320, 480)];
  • Create two methods within ShellViewController
    • - (void)presentSelf
    • - (void)dismissSelf
  • Create an instance of ShellViewController when you want to present your UITabBarController
  • Place your UITabBarController instance inside of the ShellViewController instance
  • Call [currentView addSubview:shellViewController.view];
  • Use the custom methods above to present and dismiss the ShellViewController housing your UITabBarController
  • Deal with memory management as your business logic dictates

Here is the code for animating-in (e.g. the - (void)presentSelf method):

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15]; //the double represents seconds
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[self view] setFrame:CGRectMake(0, 0, 320, 480)];
[UIView commitAnimations];

Here is the code for animating-out (e.g. the - (void)dismissSelf method):

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[self view] setFrame:CGRectMake(320, 0, 320, 480)];
[UIView commitAnimations];

Keep in mind that these animation methods do only that: animate. They don't disable interaction with the current view nor with the ShellViewController's view/subviews which are being animated in/out. You'll need to manually disable user interaction during animation and then reinstate it after the animation is finished. There is a UIView method that performs a selector when animation is finished:

[UIView setAnimationDidStopSelector:@selector(enableUserInteraction)];

You can put this right after the [UIView setAnimationDelegate:self] in each animation block above. Of course, you would need to write the enableUserInteraction method yourself... and disableUserInteraction method for that matter.

It is a hassle to go this route, but it works. Once you get the ShellViewController written up, it makes for a nice reusable snippet.



回答2:

Embed your root tab bar controller in a UINavigationController. If it doesn’t let you, stick a UIViewController in between them (making: UINavigationController embeds UIViewController, which has a UITabBarController’s view added to it). It’s nasty-bad, but it ought (!) to work.



回答3:

Modal view controllers don't slide in because that's the standard stack animation (pushing or popping). That's confusing to users. If it's modal you really should slide it up from the bottom or do a flip or something.

Why do you need a tab bar on a modal view? Typically modal views are used for things like data entry, audio playback, etc. The tab bar HIG states "in general, use a tab bar to organize information at the application level". Having a tab bar in a modal view controller breaks that. Granted one doesn't have to follow every guideline in the HIG exactly but this is a case where you really should consider following Apple's advice.

Can you tell us more about your specific use case so we can make suggestions on what might be an appropriate solution? Perhaps a segmented control is more appropriate?