使用uitabbarcontroller动画视图更改

时间:2011-07-13 04:10:52

标签: iphone ios animation uitabbarcontroller

我想动画我的视图更改,但是我不知道在哪里放动我的动画,因为我有一个UITabBarController控制我在哪个视图以及我可以切换的内容..这是在我的appdelegate.m中声明的根据xcode模板提交文件。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

或者他们是每个视图控制器中所有内容都被加载的区域......我认为在这里初始化动画可能会更好。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Super", @"Super");
        self.tabBarItem.image = [UIImage imageNamed:@"SuperIcon"];
    }
    return self;
}

由于我没有做过很多带有视图的动画,所以我在讨论在哪里可以声明这个代码片段,希望在触摸UITabBarButton时动画我的视图变化....

[UIView transitionWithView:containerView
           duration:0.2
           options:UIViewAnimationOptionTransitionFlipFromLeft
           animations:^{ [fromView removeFromSuperview]; [containerView addSubview:toView]; 
           completion:NULL];

还是我完全错过了这艘船,是他们应该做的其他地方吗?

2 个答案:

答案 0 :(得分:2)

在viewWillAppear方法中尝试以下代码

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                       forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];

这适用于基于tabBar控制器的应用程序。

答案 1 :(得分:1)

在viewWillAppear方法的代码中尝试该动画