自定义Tabbar实现push动画隐藏效果

来源:互联网 发布:软件控制路由功能 编辑:程序博客网 时间:2024/06/05 13:26


自定义Tabbar实现push动画隐藏效果


如果要实现自定义tabbar在每次push viewController时隐藏,很显然我们需要push时有事件能够通知自定义tab bar隐藏。如果你熟悉UINavigationController的push流程的话,应该就知道我们可以让UINavigationController执行 push时调用navigationController: willShowViewController:方法来触发通知,前提是要遵守UINavigationControllerDelegate协议。 由于hidesBottomBarWhenPushed是每个UIViewController都有的属性,我们姑且还是把它用上。代码如下:

Here’s Code
12345678910
- (void)navigationController:(UINavigationController *)navigationController  willShowViewController:(UIViewController *)viewController                animated:(BOOL)animated{    if (viewController.hidesBottomBarWhenPushed) {        self.tabBar.hidden = YES;    } else {        self.tabBar.hidden = NO;    }}

这样的实现是比较简单的。对比weico或微信iPhone应用的自定义tab bar push隐藏行为,你就会发现它们有一个自然的过滤动画来实现隐藏,而且与viewController的push动画同步,这是上面的代码做不到的。如果要实现这个动画,就需要对self.tabbar设置frame的过渡动画,代码如下:

Here’s Code
123456789101112131415161718192021222324252627282930313233343536373839
- (void)navigationController:(UINavigationController *)navController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{    if (viewController.hidesBottomBarWhenPushed)    {        [self hideTabBar];    }    else    {        [self showTabBar];    }}- (void)hideTabBar {    if (!tabBarIsShow)    { //already hidden        return;    }    [UIView animateWithDuration:0.35                     animations:^{                         CGRect tabFrame = tabBar.frame;                         tabFrame.origin.x = 0 - tabFrame.size.width;                         tabBar.frame = tabFrame;                     }];    tabBarIsShow = NO;}- (void)showTabBar {    if (tabBarIsShow)    { // already showing        return;    }    [UIView animateWithDuration:0.35                     animations:^{                         CGRect tabFrame = tabBar.frame;                         tabFrame.origin.x = CGRectGetWidth(tabFrame) + CGRectGetMinX(tabFrame);                         tabBar.frame = tabFrame;                     }];    tabBarIsShow = YES;}

上面代码中的0.35秒这个时间保证了与tabbar的隐藏动画与viewController的push动画同步,基本上可以实现以假乱真的效果。

0 0