Skip to content

Instantly share code, notes, and snippets.

@tailang
Last active June 22, 2016 09:05
Show Gist options
  • Save tailang/8657314f4e641367366fae8b2b91bd4c to your computer and use it in GitHub Desktop.
Save tailang/8657314f4e641367366fae8b2b91bd4c to your computer and use it in GitHub Desktop.
UIViewController容器Demo
//
//  MenuChartsViewContainerController.m
//  CardApp
//
//  Created by tailang on 6/22/16.
//  Copyright © 2016 2dfire.com. All rights reserved.
//

#import "MenuChartsViewContainerController.h"
#import "ShopMenuChartsViewController.h"
#import "MyMenuChartsViewController.h"

@interface MenuChartsViewContainerController ()

@property (nonatomic, strong) ShopMenuChartsViewController *shopMenuChartsViewController;
@property (nonatomic, strong) MyMenuChartsViewController *myMenuChartsViewController;
@property (nonatomic, strong) UIViewController *currentViewController;

@property (nonatomic, strong) UIButton *shopMenuChartsButton;
@property (nonatomic, strong) UIButton *myMenuChartsButton;

@end

@implementation MenuChartsViewContainerController

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.shopMenuChartsButton];
    [self.view addSubview:self.myMenuChartsButton];
    
    //初始化显示 店家排行榜
    [self addChildViewController:self.shopMenuChartsViewController];
    [self.view addSubview:self.shopMenuChartsViewController.view];
    self.currentViewController = self.shopMenuChartsViewController;
    
    [self setLayout];
    
}


#pragma mark - @@@systemDelegate
#pragma mark - @@@customDelegate
#pragma mark - event response methods
- (void)switchButton:(UIButton *)sender {
    
    BOOL isCurrentShopMenuCharts = self.currentViewController == self.shopMenuChartsViewController && sender.tag == 10001;
    BOOL isCurrentMyMenuCharts = self.currentViewController == self.myMenuChartsViewController && sender.tag == 10002;
    
    if (isCurrentMyMenuCharts || isCurrentShopMenuCharts) {
        return;
    }
    
    switch (sender.tag) {
        case 10001: {
            [self moveFromeViewController:self.currentViewController toViewController:self.shopMenuChartsViewController];
            self.currentViewController = self.shopMenuChartsViewController;
        }
            break;
            
        case 10002: {
            [self moveFromeViewController:self.currentViewController toViewController:self.myMenuChartsViewController];
            self.currentViewController = self.myMenuChartsViewController;
        }
            break;
        default:
            break;
    }

}

#pragma mark - request methods
#pragma mark - public methods
#pragma mark - private methods
- (void)moveFromeViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController {
    [self addChildViewController:toViewController];
    [fromViewController willMoveToParentViewController:nil];
    
    [self transitionFromViewController:fromViewController
                      toViewController:toViewController
                              duration:0.1
                               options:UIViewAnimationOptionTransitionNone
                            animations:nil
                            completion:^(BOOL finished) {
                                [toViewController didMoveToParentViewController:self];
                                [fromViewController removeFromParentViewController];
                            }];
}


- (void)setLayout {
    [self.shopMenuChartsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.centerX.equalTo(self.view).offset(-75);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    [self.myMenuChartsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.centerX.equalTo(self.view).offset(75);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
}

#pragma mark - getters and setters methods

- (UIButton *)shopMenuChartsButton {

    if (!_shopMenuChartsButton) {
        _shopMenuChartsButton = [UIButton createWithType:UIButtonTypeCustom
                                            cornerRadius:0
                                                   color:nil
                                                   image:[UIImage imageNamed:@"icon_open_eye"]
                                                  target:self
                                                  action:@selector(switchButton:)];
        _shopMenuChartsButton.tag = 10001;
    }
    
    return _shopMenuChartsButton;
    
}

- (UIButton *)myMenuChartsButton {
    
    if (!_myMenuChartsButton) {
        _myMenuChartsButton = [UIButton createWithType:UIButtonTypeCustom
                                          cornerRadius:0
                                                 color:nil
                                                 image:[UIImage imageNamed:@"icon_open_eye"]
                                                target:self
                                                action:@selector(switchButton:)];
        
        _myMenuChartsButton.tag = 10002;
    }
    
    
    return _myMenuChartsButton;
    
}

- (ShopMenuChartsViewController *)shopMenuChartsViewController {
    
    if (!_shopMenuChartsViewController) {
        _shopMenuChartsViewController = [[ShopMenuChartsViewController alloc] init];
        _shopMenuChartsViewController.view.frame = CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height - 100);
    }
    
    return _shopMenuChartsViewController;
    
}

- (MyMenuChartsViewController *)myMenuChartsViewController {
    
    if (!_myMenuChartsViewController) {
        _myMenuChartsViewController = [[MyMenuChartsViewController alloc] init];
        _myMenuChartsViewController.view.frame = CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height - 100);
    }
    
    return _myMenuChartsViewController;
    
}

@end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment