2017.03.26

精华页面基本框架

精华.gif

该页面中间部分为一个view加一个scrollView.view上面添加了五个按钮,点击按钮时,下面的scrollView跟随着滚动.当滚动下面的scrollView的时候,上面的按钮也跟随着变化相应的模式.

/**
 添加子控制器
 */
- (void)setupChildVc {
    
    LXXAllViewController *allVc = [[LXXAllViewController alloc] init];
    [self addChildViewController:allVc];
    
    LXXVedioViewController *vedioVc = [[LXXVedioViewController alloc] init];
    [self addChildViewController:vedioVc];
    
    LXXVoiceViewController *voiceVc = [[LXXVoiceViewController alloc] init];
    [self addChildViewController:voiceVc];
    
    LXXPictureViewController *picVc = [[LXXPictureViewController alloc] init];
    [self addChildViewController:picVc];
    
    LXXWordViewController *wordVc = [[LXXWordViewController alloc] init];
    [self addChildViewController:wordVc];

}
/**
 设置导航栏
 */
- (void)setupNav {
    
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    //设置左上角
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" highImage:@"MainTagSubIconClick" target:self action:@selector(mainTagSubClick)];
    
    self.view.backgroundColor = LXXCommonBgColor;

}
/**
 添加titleView
 */
- (void)setupTitleView {

    //创建titleView
    CGFloat titleViewW = KScreenW;
    CGFloat titleViewH = 35;
    CGFloat titleViewY = 64;
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, titleViewY, titleViewW, titleViewH)];
    titleView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    self.titleView = titleView;
    [self.view addSubview:titleView];
    
    //创建底部显示view
    CGFloat indicatorViewH = 2;
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.y = titleViewH - indicatorViewH;
    indicatorView.x = 0;
    indicatorView.tag = -1;
    indicatorView.height = indicatorViewH;
    indicatorView.backgroundColor = [UIColor redColor];
    
    self.indicatorView = indicatorView;
    
    //创建title按钮
    NSArray *titleArr = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    CGFloat titleBtnY = 0;
    CGFloat titleBtnW = KScreenW / titleArr.count;
    CGFloat titleBtnH = titleViewH;
    for (int i = 0; i < titleArr.count; i++) {
        CGFloat titleBtnX = i * titleBtnW;
        UIButton *titleBtn = [[UIButton alloc] initWithFrame:CGRectMake(titleBtnX, titleBtnY, titleBtnW, titleBtnH)];
        titleBtn.tag = i;
        titleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [titleBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        [titleBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [titleBtn setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        [titleBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:titleBtn];
        
        if (i == 0) {//加载时默认选中第一个按钮
            
            titleBtn.enabled = NO;
            self.selectedBtn = titleBtn;
            [titleBtn.titleLabel sizeToFit];
            self.indicatorView.width = titleBtn.titleLabel.width;
            self.indicatorView.centerX = titleBtn.centerX;
        }
    }
    [titleView addSubview:indicatorView];
}
/**
 监听title按钮的点击

 @param btn 点击的按钮
 */
- (void)titleBtnClick:(UIButton *)btn {

    self.selectedBtn.enabled = YES;
    btn.enabled = NO;
    self.selectedBtn = btn;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.indicatorView.width = btn.titleLabel.width;
        self.indicatorView.centerX = btn.centerX;
        
    }];
    
    // 中间内容滚动
    CGPoint offset = self.contentView.contentOffset;
    offset.x = btn.tag * self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
    
}
/**
 设置中间的内容的view
 */
- (void)setupContentView {
    
    // 不要自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *contentView = [[UIScrollView alloc] init];
    contentView.frame = self.view.bounds;
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
    self.contentView = contentView;
    [self.view insertSubview:contentView atIndex:0];
    
    // 添加第一个控制器的view
    [self scrollViewDidEndScrollingAnimation:contentView];

}
#pragma mark - scrollView代理方法

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

    // 当前的索引
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    // 取出子控制器
    UITableViewController *vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0;
    vc.view.height = scrollView.height;
    
    // 设置内边距
    CGFloat bottom = self.tabBarController.tabBar.height;
    CGFloat top = CGRectGetMaxY(self.titleView.frame);
    vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    
    // 设置滚动条的内边距
    vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    [scrollView addSubview:vc.view];

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    // 点击按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    [self titleBtnClick:self.titleView.subviews[index]];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,386评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,142评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,704评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,702评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,716评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,573评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,314评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,230评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,680评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,873评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,991评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,706评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,329评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,910评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,038评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,158评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,941评论 2 355

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,140评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,104评论 4 62
  • 构造函数的原型对象 什么是原型对象 每个构造函数默认都有一个和它相关联(<->)的对象,这个对象被称为构造函数的原...
    GodlinE阅读 161评论 0 0
  • 大学语文2017年备考题库 一、作家作品知识 (共75题) 1. 有“一代文宗”之美誉的文学家是(D ) A.曹操...
    jgmw阅读 2,077评论 0 0
  • 纯手工制作,独一无二,哈哈
    闹闹mam阅读 283评论 0 0