How to hide tab bar with animation in iOS?

后端 未结 15 1894
夕颜
夕颜 2020-11-30 19:20

So I have a button that is connected to a IBAction. When I press the button I want to hide the tab bar in my iOS app with a animation. This [self setTabBarHidden:hidde

15条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 20:03

    I try to keep view animations available to me using the following formula:

    // pass a param to describe the state change, an animated flag and a completion block matching UIView animations completion 
    - (void)setTabBarVisible:(BOOL)visible animated:(BOOL)animated completion:(void (^)(BOOL))completion {
    
        // bail if the current state matches the desired state
        if ([self tabBarIsVisible] == visible) return (completion)? completion(YES) : nil;
    
        // get a frame calculation ready
        CGRect frame = self.tabBarController.tabBar.frame;
        CGFloat height = frame.size.height;
        CGFloat offsetY = (visible)? -height : height;
    
        // zero duration means no animation
        CGFloat duration = (animated)? 0.3 : 0.0;
    
        [UIView animateWithDuration:duration animations:^{
            self.tabBarController.tabBar.frame = CGRectOffset(frame, 0, offsetY);
        } completion:completion];
    }
    
    //Getter to know the current state
    - (BOOL)tabBarIsVisible {
        return self.tabBarController.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame);
    }
    
    //An illustration of a call to toggle current state
    - (IBAction)pressedButton:(id)sender {
        [self setTabBarVisible:![self tabBarIsVisible] animated:YES completion:^(BOOL finished) {
            NSLog(@"finished");
        }];
    }
    

提交回复
热议问题