iPhone在使用tabbar时如何使用转换动画

jopen 11年前

参考《iphone3 开发基础教程》这本书的写了一个例子,用到了UITabBarController,但是发现一个问题,教程提供的例子里切换界面时没有动画效果,体验效果很不好。之前的例子在添加动画时,界面的跳转是由自己来实现的,类似下面的代码:

[yellowViewController.view removeFromSuperview];         CATransition* animation = [CATransition animation];    [animation setDuration:0.5f];    [animation setType:kCATransitionPush];    [animation setSubtype:kCATransitionFromRight];    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];    [[self.view layer]addAnimation:animation forKey:@"switchView"];         [self.view insertSubview:blueViewController.view atIndex:0];

将动画的代码放在两个界面切换的中间就行了。

 

但是这里界面切换的实现全部由UITabBarController,也就是系统自动帮你来实现了,要在哪里添加动画代码呢?

很简单,写一个类继承自UITabBarController,然后实现下面这个委托方法就行了:

-(void)tabBar:(UITabBar*)atabBar didSelectItem:(UITabBarItem*)item    {        CATransition* animation = [CATransition animation];        [animation setDuration:0.5f];        [animation setType:kCATransitionFade];        [animation setSubtype:kCATransitionFromRight];        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];        [[self.view layer]addAnimation:animation forKey:@"switchView"];    }