iPhone - Animate TabBarController开关

时间:2010-03-04 15:04:07

标签: iphone animation sdk uitabbarcontroller

我正在通过代码切换标签:

self.tabBarController.selectedIndex = 1; // or any other number

标签之间的切换工作正常。我想知道是否有办法动画过渡。它必须只是在这种情况下,而不是一般的tabbarcontroller。也就是说,转换仅在选项卡切换到特定代码段时才会发生。单击实际标签项时,它应该是即时的(没有动画)。

如果有人可以提供帮助,我们非常感谢帮助。

1 个答案:

答案 0 :(得分:1)

如果您没有使用标签栏控制器进行任何复杂操作,您可以从UITabBar中自行滚动,然后在切换标签时可以执行任何操作。您只需实施UITabBarDelegate即可。以下是我在应用程序中使用的示例代码:

.h文件:

@interface AllGames : UIViewController <UITabBarDelegate> {
    IBOutlet UITabBar *tabBar;
}
@property (nonatomic, retain) IBOutlet UITabBar *tabBar;

.m文件:

@implementation AllGames

@synthesize tabBar;

UIViewController *subviews[3];

- (void)viewDidLoad {
    [super viewDidLoad];

    int startTab = [Preferences LookupPreferenceInt:CFSTR("GameTab")];
    [tabBar setSelectedItem:[tabBar.items objectAtIndex:startTab]];

    Games1 *vc1 = [[Games1 alloc] initWithNibName:@"Games1" bundle:nil];
    Games2 *vc2 = [[Games2 alloc] initWithNibName:@"Games2" bundle:nil];
    Games3 *vc3 = [[Games3 alloc] initWithNibName:@"Games3" bundle:nil];

    subviews[0] = vc1;
    subviews[1] = vc2;
    subviews[2] = vc3;
    vc1.view.alpha = 0;
    vc2.view.alpha = 0;
    vc3.view.alpha = 0;
    [self.view addSubview:vc1.view];
    [self.view addSubview:vc2.view];
    [self.view addSubview:vc3.view];
    subviews[startTab].view.alpha = 1.0;
}

- (void)dealloc {
    [Preferences StorePreferenceInt:CFSTR("GameTab") withValue:[tabBar selectedItem].tag-1];
    for (int x = 0; x < 3; x++)
    {
        [subviews[x].view removeFromSuperview];
        [subviews[x] release];
        subviews[x] = 0;
    }
    [super dealloc];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    printf("Hit item tag: %d\n", item.tag);
    for (int x = 1; x <= 3; x++)
    {
        subviews[x-1].view.alpha = (x == item.tag)?1.0:0.0;
    }
}

在didSelectItem中你可以做任何你想要的动画。我只是立即交换alpha,但你可以像你想的那样复杂。