【问题标题】:iPhone - Animate TabBarController SwitchesiPhone - 动画 TabBarController 开关
【发布时间】:2010-03-04 15:04:07
【问题描述】:
我正在通过代码切换标签:
self.tabBarController.selectedIndex = 1; // or any other number
标签之间的切换工作正常。我想知道是否有办法为过渡设置动画。它必须只在这种情况下,而不是一般的总 tabbarcontroller。也就是说,只有在通过该特定代码段切换选项卡时才会发生转换。单击实际选项卡项时,它应该是瞬时的(没有动画)。
如果有人可以提供帮助,我们将不胜感激。
【问题讨论】:
标签:
iphone
animation
sdk
uitabbarcontroller
【解决方案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,但你可以随意变复杂。