2010-03-04 15 views
0

我通過代碼切換標籤與此:iPhone - 動畫TabBarController開關

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

標籤之間的切換工作正常。我想知道是否有辦法讓這個過渡動畫化。它只能在這個例子中,而不是一般的tabbarcontroller。也就是說,只有當標籤通過特定的一段代碼進行切換時纔會進行轉換。當實際標籤項被點擊時,它應該是瞬時的(沒有動畫)。

如果有人能幫助,非常感謝幫助。

回答

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,但你可以像你想的那樣複雜。

+0

你有什麼例子嗎?它會幫助我瞭解要做什麼。 – intl 2010-03-05 14:29:17

+0

用一個詳細的例子編輯上面的代碼。 – 2010-03-05 16:32:07