17-ScrollView(联动)

1.png

需求:当点击标题栏的标题时,tableView跳转到相应的控制器

分析:其实就是控制scrollview的contentOffset

具体分析:
(1)将UIScrollView添加到精华控制器的view上
(2)将子控制器view添加到UIScrollView上,但是不要一次全部加载进来
解决:即先初始化子控制器,在往UIScrollView添加子控制器的view

相关问题:

(1)
问题:有时tableView的分割线会消失,右边出现蓝色分割线
原因:模拟器的问题,屏幕小的时候会有,放大就不见了

(2)
问题:将tableView添加到scrollview上,scrollview会自动在上方空出64的距离
原因:2个
第一个原因: 不是所有的控制器frame的x,y值都是0。

  • UIViewController.view.frame:
    (origin = (x = 0, y = 0), size = (width = 375, height = 667))
  • UITableViewController.view.frame:
    (origin = (x = 0, y = 20), size = (width = 375, height = 667))

解决:要将子控制器的y值设为0,但是这样提上去,下面就会少20,故还要设置子控制器的高度。
childView.frame = CGRectMake(index * self.scrollView.xmg_width,0, self.scrollView.xmg_width, self.scrollView.xmg_height);

第二个原因:scrollview自己会自动留出44的内边距(为导航条)。
解决:让tableView占据scrollview全屏,不允许自动修改scrollview的内边距
self.automaticallyAdjustsScrollViewInsets = NO;

(3)设置scrollview分页
scrollView.pagingEnabled = YES;

(4)
问题:设置标题栏和tabBar都有穿透效果(cell的全屏穿透效果)

  • tableView占据整个屏幕
  • 设置tableView的内边距,防止当cell向下拽时,弹回被导航栏或tabBar遮挡
    self.tableView.contentInset = UIEdgeInsetsMake(64 + 35, 0, 49, 0);

(5)
问题:当拖到下一个控制器时,标题按钮相对应的也要移动
解决:监听scrollview的滑动,如何监听?

  • 成为scrollview的代理
  • 代理方法:
    - (void)scrollViewDidEndDecelerating:滑动完停稳后调用
    -(void)scrollViewDidScroll:只要滑动一点点就调用
    -(void)scrollViewDidEndDragging:当用户松开scrollview,拖拽结束调用

问题:scrollView停稳后,如何知道对应的标题按钮?
解决:先获得标题按钮的索引(index)
index = scrollview的偏移量 / scrollview的宽度

(6) 在创建标题按钮时,给按钮绑定tag,方便通过tag取出子控件。

(7)
问题:用户一直向上滑动tableview,滑动到最后,想回到最开始
解决:苹果自带的功能:点击状态栏,就会回到最前面,这是ScrollView的特性,但是在这里并不起作用,为什么呢?
原因:ScrollViewToTop这个属性没起作用,这个功能有效的前提是只有一个子控制器的这个属性是YES,其余的是NO才可以。
例子:有4个子控制器 ,这样这个功能才会有效
S1 ........ ScrollViewToTop.........NO
S2 ........ ScrollViewToTop.........NO
S3 ........ ScrollViewToTop.........NO
S4 ........ ScrollViewToTop.........YES
解决: 设置index位置对应的tableView.scrollsToTop = YES, 其他都设置为NO

代码:

#import "BSEssenceController.h"
#import "UIBarButtonItem+BSExtension.h"
#import "BSTagController.h"
#import "BSAllTableViewController.h"
#import "BSVideoTableViewController.h"
#import "BSPictureTableViewController.h"
#import "BSWordTableViewController.h"
#import "BSVoiceTableViewController.h"
#import "BSTitleButton.h"

@interface BSEssenceController () <UIScrollViewDelegate>
/** 标题栏*/
@property (nonatomic ,weak) UIView *titleView;
/** 上一次点击的按钮*/
@property (nonatomic ,weak) BSTitleButton *previousBtn;
/** 下划线*/
@property (nonatomic ,weak) UIView *titleUnderLine;
/** scrollview*/
@property (nonatomic ,weak) UIScrollView *scrollView;

@end

@implementation BSEssenceController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // 初始化子控制器
    [self setupChildControllers];
    // 导航条
    [self setupNav];
    // scrollView
    [self setupScrollView];
    // 标题栏
    [self setupTitleView];
    
    // 添加第0个控制器的view
    [self addChildViewToScrollView:0];
}

/**
 *  初始化子控制器
 */
- (void)setupChildControllers
{
    [self addChildViewController:[[BSAllTableViewController alloc] init]];
    [self addChildViewController:[[BSWordTableViewController alloc] init]];
    [self addChildViewController:[[BSVideoTableViewController alloc] init]];
    [self addChildViewController:[[BSPictureTableViewController alloc] init]];
    [self addChildViewController:[[BSVoiceTableViewController alloc] init]];
}

/**
 *  ScrollView
 */
- (void)setupScrollView
{
    // 不允许自动修改UIScrollView的内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.backgroundColor = [UIColor blueColor];
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.scrollsToTop = NO;   // 点击状态栏时,这个scrollView不会滚动到最顶部
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    // 添加自控制器view
    NSUInteger count = self.childViewControllers.count;
    scrollView.contentSize = CGSizeMake(count * scrollView.xmg_width, 0);
}

/**
 *  标题栏
 */
- (void)setupTitleView
{
    // UIView
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 44+20, self.view.xmg_width, 35)];
    titleView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    // 标题按钮
    [self setupTitleButtons];
    
    // 标题下划线
    [self setupTitleUnderLine];
}

/**
 *  标题栏按钮
 */
- (void)setupTitleButtons
{
    // 文字
    NSArray *titles = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    NSUInteger count = titles.count;
    for (NSUInteger i = 0; i < count; i++) {
        BSTitleButton *titleBtn = [[BSTitleButton alloc] init];
        titleBtn.tag = i;
        // frame
        CGFloat titleBtnW = self.titleView.xmg_width / count;
        CGFloat titleBtnH = self.titleView.xmg_height;
        titleBtn.frame = CGRectMake(i * titleBtnW, 0, titleBtnW, titleBtnH);
   //     titleBtn.backgroundColor = BSRandomColor;
        [titleBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        // 将文字添加到对应btn上
        [titleBtn setTitle:titles[i] forState:UIControlStateNormal];
        [self.titleView addSubview:titleBtn];
    }
}

/**
 *  标题栏下划线
 */
- (void)setupTitleUnderLine
{
   BSTitleButton *firstbtn = self.titleView.subviews.firstObject;
    // 下划线
    UIView *line = [[UIView alloc] init];
    line.xmg_height = 2;
    line.xmg_y = self.titleView.xmg_height - line.xmg_height;
    line.backgroundColor = [firstbtn titleColorForState:UIControlStateSelected];
    [self.titleView addSubview:line];
    self.titleUnderLine = line;
    
    // 第一个按钮状态
    firstbtn.selected = YES;
    self.previousBtn = firstbtn;
    
    [firstbtn.titleLabel sizeToFit];  // 让label根据文字内容计算尺寸
    self.titleUnderLine.xmg_width = firstbtn.titleLabel.xmg_width;
    self.titleUnderLine.xmg_centerX = firstbtn.xmg_centerX;
}

#pragma mark 监听
-(void)btnClick:(BSTitleButton *)button
{
    // 切换按钮状态
    self.previousBtn.selected = NO;
    button.selected = YES;
    self.previousBtn = button;
    
    NSUInteger index = button.tag;
    // 下划线动画
    [UIView animateWithDuration:0.25 animations:^{
        // 处理下划线
        self.titleUnderLine.xmg_width = button.titleLabel.xmg_width;
        self.titleUnderLine.xmg_centerX = button.xmg_centerX;
        
        // 滚动scrollView
        CGFloat offsetX = index * self.scrollView.xmg_width;
        self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.xmg_y);
    } completion:^(BOOL finished) {
        // 添加子控制器的view
        [self addChildViewToScrollView:index];
    }];

    // 设置index位置对应的tableView.scrollsToTop = YES, 其他都设置为NO
    for (NSUInteger i = 0; i < self.childViewControllers.count; i++) {
        // 如果view还没有被创建,就不用去处理
        UIViewController *childVC = self.childViewControllers[i];
        if (!childVC.isViewLoaded) return;
        UIScrollView *scrollView = (UIScrollView *)childVC.view;
        // 判断scrollView是不是UIScrollView类型,不是就跳过
        if (![scrollView isKindOfClass:[UIScrollView class]]) continue;
        // 当控制器【i】和按钮【tag】一致时,才设置。
        scrollView.scrollsToTop = (i == index);
    }
  }
}

#pragma mark - <UIScrollViewDelegate>
/**
 *  监听滚动,获得索引(停稳后调用)
 */
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSUInteger index = scrollView.contentOffset.x / scrollView.xmg_width;
    
    // 点击对应的标题按钮
    BSTitleButton *titleButton = self.titleView.subviews[index];
    [self btnClick:titleButton];
}

/**
 *  根据索引,添加对应的view
 */
- (void)addChildViewToScrollView : (NSUInteger)index;
{
    //取出索引对应的控制器
    UIViewController *childVC = self.childViewControllers[index];
    
    // 如果view已经加载过,就直接返回
    if (childVC.isViewLoaded) return;
    
    // 去除index对应的view
    UIView *childView = childVC.view;
    childView.frame = CGRectMake(index * self.scrollView.xmg_width,0, self.scrollView.xmg_width, self.scrollView.xmg_height);
    [self.scrollView addSubview:childView];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容