动画相关

NavigationBar 和StatusBar同时消失出现

2016-02-25  本文已影响652人  RY_zheng

NavigationBar和StatusBar都有支持动画的系统接口:

[[UIApplication sharedApplication] setStatusBarHidden:hidden withAnimation:UIStatusBarAnimationSlide];
[self.navigationController setNavigationBarHidden:hidden animated:animated];

但是简单这样写的话,效果不太好,NavigationBar 和StatusBar消失的时候,动画太快;出现的时候,StatusBar先出现,NavigationBar后滑出,而且动画比较突兀。

以下代码能让NavigationBar 和StatusBar同时消失出现,而且比较平滑 :

- (void)toggleStatusBarAndNavBar:(BOOL)hidden {
    UINavigationBar *navBar = self.navigationController.navigationBar;
    [[UIApplication sharedApplication] setStatusBarHidden:hidden withAnimation:UIStatusBarAnimationSlide];
    
    [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        // 先显示navigationBar
        if (!hidden)
        {
            [self.navigationController setNavigationBarHidden:hidden animated:NO];
        }
        
        navBar.frame = CGRectMake(navBar.frame.origin.x,
                                  hidden ? -navBar.frame.size.height : 20,
                                  navBar.frame.size.width,
                                  navBar.frame.size.height);
    } completion:^(BOOL finished) {
        if (hidden)
        {
            [self.navigationController setNavigationBarHidden:hidden animated:NO];
        }
    }];
}
上一篇下一篇

猜你喜欢

热点阅读