iPhone - 动画 TabBarController 开关

发布于 2024-08-23 18:34:05 字数 266 浏览 3 评论 0原文

我通过代码切换选项卡:

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

选项卡之间的切换工作正常。我想知道是否有一种方法可以使过渡动画化。它必须仅在本例中,而不是一般的整个 tabbarcontroller。也就是说,仅当通过该特定代码段切换选项卡时才会发生转换。当单击实际选项卡项时,它应该是瞬时的(没有动画)。

如果有人可以提供帮助,我们将不胜感激。

I am switching tabs through code with this:

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

The switching between the tabs works fine. I'm wondering if there is a way to animate the transition. It'd have to be only in this instance, not the total tabbarcontroller in general. That is to say that the transition will only take place when the tab is switched through that specific piece of code. It should be instantaneous(without animation) when the actual tab item is clicked.

If anyone can help, help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-08-30 18:34:05

如果标签栏控制器没有任何复杂的操作,您可以从 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,但你可以随心所欲地复杂化。

If you don't have anything complicated going on with the tab bar controller, you can roll your own from a UITabBar and then you can do anything you want when you switch tabs. You just implement the UITabBarDelegate. Here is example code slightly modified from what I'm using in an app:

.h file:

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

.m file:

@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;
    }
}

Inside didSelectItem you can do any animation you want. I just do an immediate swap of the alpha, but you can be as complicated as you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文