iOS7 Side menu status bar color transition. As in the iOS7 Facebook App

前端 未结 3 1143
清酒与你
清酒与你 2021-02-01 10:53

The iOS7 Facebook App has a right side menu that can be shown by swiping right to left or clicking on the upper right button. When this menu is opened the there is a color trans

3条回答
  •  我在风中等你
    2021-02-01 11:34

    I managed to find a very simple, elegant way to do this, that mimics the Facebook app functionality perfectly.

    Here's my approach:

    • Create view with status bar frame
    • Set view background color to black, opacity to 0
    • Add view as subview to any root view (you need a view that will cover both the center view and the menus, so that it won't be confined to any single view - a good option for this is the container view controller used by your menu controller implementation)
    • Set view's opacity in your menu controller implementation's menu animation method

    Here's my specific implementation, using MMDrawerController:

    I subclassed MMDrawerController (I actually already had a subclass for using MMDrawerController with storyboards), and added this code to the class's init method:

    // Setup view behind status bar for fading during menu drawer animations
    if (OSVersionIsAtLeastiOS7()) {
        self.statusBarView = [[UIView alloc] initWithFrame:[[UIApplication sharedApplication] statusBarFrame]];
        [self.statusBarView setBackgroundColor:[UIColor blackColor]];
        [self.statusBarView setAlpha:0.0];
        [self.view addSubview:self.statusBarView];
    }
    
    // Setup drawer animations
    __weak __typeof(&*self) weakSelf = self; // Capture self weakly
    
    [self setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {
        MMDrawerControllerDrawerVisualStateBlock block;
        block = (drawerSide == MMDrawerSideLeft) ? [MMDrawerVisualState parallaxVisualStateBlockWithParallaxFactor:15.0] : nil; // Right side animation : Left side animation
        if(block){
            block(drawerController, drawerSide, percentVisible);
        }
        [weakSelf.statusBarView setAlpha:percentVisible];    // THIS IS THE RELEVANT CODE
    }];
    

    I also added self.statusBarView as a private property.

    • The first section of code creates a view, configures it, and adds it as a subview of the MMDrawerController subclass's view. The OSVersionIsAtLeastiOS7() method is a custom method that simplifies the check to see if the device is running iOS 7 (if it isn't, your custom view will show up below the status bar, which you don't want).

    • The second section of code is MMDrawerController's setDrawerVisualStateBlock method, which sets the animations code to be performed when a menu is being opened and closed. The first few lines of code are boilerplate code that sets one of the prebuilt animations blocks to each menu (I wanted parallax on the left, but nothing on the right). The relevant code is the last line of the block: [weakSelf.statusBarView setAlpha:percentVisible];, which sets the status bar view's opacity to match the percentage that the menu is currently open. This allows for the smooth cross animation you see in the Facebook app. You'll also notice I've assigned self to a variable weakSelf, so as to avoid the "retain cycle" compiler warning.

    This is my specific approach using MMDrawerController and a subclass, which I did more for convenience because I already had the subclass in place, than because it is necessarily the best approach or the only way to do it. It could probably be implemented in several other ways, using MMDrawerController without a subclass, or using any other side-drawer menu implementation.

    The ending result is a smooth fading to black animation behind the status bar, exactly as you see in the new Facebook app.

提交回复
热议问题