美文网首页
bsbdj-XMGEssenceViewController

bsbdj-XMGEssenceViewController

作者: CaesarsTesla | 来源:发表于2016-05-06 16:40 被阅读78次

    XMGEssenceViewController

    屏幕快照 2016-05-06 下午3.25.59.png

    精华页面里有5个子控制器(全部,视频,声音,图片,段子)这5个控制器都是XMGTopicViewController,只不过其中显示的cell的样式是不一样的。并且这5个控制器是可以通过手势来进行切换的,或者是通过上边的(全部,视频,声音,图片,段子)的按钮进行点击切换

    @interface XMGEssenceViewController() <UIScrollViewDelegate>
    /** 标签栏底部的红色指示器 */
    @property (nonatomic, weak) UIView *indicatorView;
    /** 当前选中的按钮 */
    @property (nonatomic, weak) UIButton *selectedButton;
    /** 顶部的所有标签 */
    @property (nonatomic, weak) UIView *titlesView;
    /** 底部的所有内容 */
    @property (nonatomic, weak) UIScrollView *contentView;
    @end
    

    -(void)viewDidLoad

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // 设置导航栏
        [self setupNav];
    
        // 初始化子控制器
        [self setupChildVces];
    
        // 设置顶部的标签栏
        [self setupTitlesView];
    
        // 底部的scrollView
        [self setupContentView];
    }
    

    初始化子控制器,就是那5个子控制器,

     - (void)setupChildVces
    {
    XMGTopicViewController *all = [[XMGTopicViewController alloc] init];
    all.title = @"全部";
    all.type = XMGTopicTypeAll;
    [self addChildViewController:all];
    
    XMGTopicViewController *video = [[XMGTopicViewController alloc] init];
    video.title = @"视频";
    video.type = XMGTopicTypeVideo;
    [self addChildViewController:video];
    
    XMGTopicViewController *voice = [[XMGTopicViewController alloc] init];
    voice.title = @"声音";
    voice.type = XMGTopicTypeVoice;
    [self addChildViewController:voice];
    
    XMGTopicViewController *picture = [[XMGTopicViewController alloc] init];
    picture.title = @"图片";
    picture.type = XMGTopicTypePicture;
    [self addChildViewController:picture];
    
    XMGTopicViewController *word = [[XMGTopicViewController alloc] init];
    word.title = @"段子";
    word.type = XMGTopicTypeWord;
    [self addChildViewController:word];
    }
    

    设置顶部的标签栏

    - (void)setupTitlesView
    {
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
    titlesView.width = self.view.width;
    titlesView.height = XMGTitilesViewH;
    titlesView.y =XMGTitilesViewY;
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    // 底部的红色指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor redColor];
    indicatorView.height = 2;
    indicatorView.tag = -1;
    indicatorView.y = titlesView.height - indicatorView.height;
    self.indicatorView = indicatorView;
    
    // 内部的子标签
    CGFloat width = titlesView.width / self.childViewControllers.count;
    CGFloat height = titlesView.height;
    for (NSInteger i = 0; i<self.childViewControllers.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = i;
        button.height = height;
        button.width = width;
        button.x = i * width;
        UIViewController *vc = self.childViewControllers[i];
        [button setTitle:vc.title forState:UIControlStateNormal];
        //[button layoutIfNeeded]; // 强制布局(强制更新子控件的frame)
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];
        
        // 默认点击了第一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectedButton = button;
            
            // 让按钮内部的label根据文字内容来计算尺寸
            [button.titleLabel sizeToFit];
            self.indicatorView.width = button.titleLabel.width;
            self.indicatorView.centerX = button.centerX;
        }
    }
    
        [titlesView addSubview:indicatorView];
    }
    

    -(void)titleClick:(UIButton *)button{}

    - (void)titleClick:(UIButton *)button
    {
    // 修改按钮状态
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    
    // 动画
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
    }];
    
    // 滚动
    CGPoint offset = self.contentView.contentOffset;
    offset.x = button.tag * self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
    }
    

    底部的scrollView

    - (void)setupContentView
    {
    // 不要自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *contentView = [[UIScrollView alloc] init];
    contentView.frame = self.view.bounds;
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    [self.view insertSubview:contentView atIndex:0];
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
    self.contentView = contentView;
    
    // 添加第一个控制器的view
    [self scrollViewDidEndScrollingAnimation:contentView];
    }
    

    设置导航栏

    - (void)setupNav
    {
    // 设置导航栏标题
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    // 设置导航栏左边的按钮
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" highImage:@"MainTagSubIconClick" target:self action:@selector(tagClick)];
    
    // 设置背景色
    self.view.backgroundColor = XMGGlobalBg;
    }
    

    -(void)tagClick

    - (void)tagClick
    {
    XMGRecommendTagsViewController *tags = [[XMGRecommendTagsViewController alloc] init];
    [self.navigationController pushViewController:tags animated:YES];
    }
    

    #pragma mark - <UIScrollViewDelegate>

    - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
    {
    // 当前的索引
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    // 取出子控制器
    UIViewController *vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0; // 设置控制器view的y值为0(默认是20)
    vc.view.height = scrollView.height; // 设置控制器view的height值为整个屏幕的高度(默认是比屏幕高度少个20)
    [scrollView addSubview:vc.view];
    }
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    // 点击按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self titleClick:self.titlesView.subviews[index]];
    }

    相关文章

      网友评论

          本文标题:bsbdj-XMGEssenceViewController

          本文链接:https://www.haomeiwen.com/subject/zwdjrttx.html