美文网首页
scrollView的滚动

scrollView的滚动

作者: March_Cullen | 来源:发表于2017-03-02 21:07 被阅读0次
import "LYEssenceViewController.h"
import "LYRecommendTagsViewController.h"
import "LYAllViewController.h"
import "LYVideoViewController.h"
import "LYVoiceViewController.h"
import "LYPictureViewController.h"
import "LYWordViewController.h"

@interface LYEssenceViewController ()<UIScrollViewDelegate>
{
    UIButton *_selecteon;
    UIView *_indicatorView;
    NSArray *_titles;
    UIScrollView *_scrollView;
    UIView *_titlesView;
}
@end

@implementation LYEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LYGlobalRGB;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setUpNav];
    // 初始化子控制器
    [self setUpChildVcs];
    [self setUpTitlesView];
    [self setUpScrollView];
}

- (void)setUpChildVcs {
    LYAllViewController *all = [[LYAllViewController alloc] init];
    [self addChildViewController:all];
    LYVideoViewController *video = [[LYVideoViewController alloc] init];
    [self addChildViewController:video];
    LYVoiceViewController *voice = [[LYVoiceViewController alloc] init];
    [self addChildViewController:voice];
    LYPictureViewController *picture = [[LYPictureViewController alloc] init];
    [self addChildViewController:picture];
    LYWordViewController *word = [[LYWordViewController alloc] init];
    [self addChildViewController:word];
}

- (void)setUpNav {
    self.navigationItem.title = @"百思不得姐";
    //    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top_navigation_middle_night"]];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"top_games_btn" highLightedImage:@"top_games_btn_pressed" target:self action:@selector(tagsBtnClick:)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"top_suiji_btn_2" highLightedImage:@"top_suiji_btn_pressed_2" target:self action:@selector(suiJiBtnClick:)];
}

- (void)tagsBtnClick:(UIButton *)btn {
    LYLogFunc;
    LYRecommendTagsViewController *tags = [[LYRecommendTagsViewController alloc] init];
    [self.navigationController pushViewController:tags animated:YES];
}

- (void)suiJiBtnClick:(UIButton *)btn {
    LYLogFunc;
}

- (void)setUpTitlesView {
    // 标签栏整体
    _titlesView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 35)];
    _titlesView.backgroundColor = LYRGBAColor(1.0, 1.0, 1.0, 0.1);
    //    titlesView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    //    titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_titlesView];
    // 内部子标签
    _titles = @[@"全部", @"视频", @"声音", @"图片", @"段子"];
    for (NSInteger i = 0; i < _titles.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * _titlesView.frame.size.width / _titles.count, 0, _titlesView.frame.size.width / _titles.count, _titlesView.frame.size.height);
        if (i == 0) {
            button.enabled = NO;
            _selecteon = button;
            [button.titleLabel sizeToFit];
        }
        [button setTitle:_titles[i] forState:UIControlStateNormal];
        [button layoutIfNeeded]; // 强制布局(强制更新子控件的frame)
        button.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 100 + i;
        [_titlesView addSubview:button];
    }
    _indicatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 40 - 2, _titlesView.frame.size.width / _titles.count, 2)];
    _indicatorView.backgroundColor = [UIColor redColor];
    _indicatorView.tag = 10;
    [_titlesView addSubview:_indicatorView];
}

- (void)buttonClick:(UIButton *)button {
    _selecteon.enabled = YES;
    button.enabled = NO;
    _selecteon = button;
    [UIView animateWithDuration:0.5 animations:^{
        _indicatorView.frame = CGRectMake(button.frame.origin.x, 40 - 2, self.view.frame.size.width / _titles.count, _indicatorView.frame.size.height);
    }];
    
    // 滚动
    CGPoint offset = _scrollView.contentOffset;
    offset.x = (button.tag - 100) * _scrollView.frame.size.width;
    [_scrollView setContentOffset:offset animated:YES];
    // 下面的方法不能实现_scrollView的滚动,应采用上面的方法
//    _scrollView.contentOffset = CGPointMake((button.tag - 100) * _scrollView.frame.size.width, 0);
}

- (void)setUpScrollView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollView.contentSize = CGSizeMake(_scrollView.width * self.childViewControllers.count, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view insertSubview:_scrollView atIndex:0];
    // 添加第一个控制器的view
    [self scrollViewDidEndScrollingAnimation:_scrollView];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    // 添加子控制器的view
    // 当前的索引
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    UITableViewController *tbVc = self.childViewControllers[index];
    tbVc.view.x = scrollView.contentOffset.x;
    // 设置内边距
    CGFloat top = CGRectGetMaxY(_titlesView.frame);
    CGFloat bottom = self.tabBarController.tabBar.height;
    tbVc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
//    tbVc.tableView.contentInset = UIEdgeInsetsMake(0, 20, 49 + 64, 0);
    [scrollView addSubview:tbVc.view];
//    [scrollView addSubview:tbVc.tableView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self buttonClick:_titlesView.subviews[index]];
//    [self buttonClick:(UIButton *)[self.view viewWithTag:scrollView.contentOffset.x / scrollView.width]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

相关文章

网友评论

      本文标题:scrollView的滚动

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