iOS 顶部视图家滚动视图联动

TOPView.h

#importtypedef void(^MainTopBlock)(NSInteger tag);

@interface RZKMainTopView : UIView

-(instancetype)initWithFrame:(CGRect)frame titleNames:(NSArray *)titles tapView:(MainTopBlock)block;

//block的名字

@property(nonatomic,copy)MainTopBlock block;

-(void)scrolling:(NSInteger)tag;

@end

TOPView.m

#import "RZKMainTopView.h"

#pragma mark -- LineView 的扩展

@interface RZKMainTopView()

@property(nonatomic,strong)UIView * lineView;

@property(nonatomic,strong)NSMutableArray * btns;

@end

@implementation RZKMainTopView

-(NSMutableArray *)btns{

if (!_btns) {

_btns = [NSMutableArray array];

}

return _btns;

}

-(instancetype)initWithFrame:(CGRect)frame titleNames:(NSArray *)titles tapView:(MainTopBlock)block{

self = [super initWithFrame:frame];

if (self) {

self.block = block;

//每个按钮的宽度

CGFloat ButtonWidth =  self.width/titles.count;

//每个按钮的高度

CGFloat ButtonHeight = self.height;

CGFloat btnx;

for (NSInteger i = 0; i < titles.coiunt;i++{

UIButton * titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];

[self.btns addObject:titleBtn];

NSString * vcName = titles[i];

//设置按钮文字

[titleBtn setTitle:vcName forState:UIControlStateNormal];

//设置按钮的颜色

[titleBtn setTintColor:[UIColor whiteColor]];

//设置字体大小

titleBtn.titleLabel.font = [UIFont systemFontOfSize:18];

btnx = i * ButtonWidth;

//设置按钮的tag

titleBtn.tag = i;

//设置frame

titleBtn.frame = CGRectMake(btnx, 0, ButtonWidth, ButtonHeight);

//设置监听 点击事件

[titleBtn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:titleBtn];

if (i == 1) {

CGFloat height = 2;

CGFloat y = 37;

[titleBtn.titleLabel sizeToFit];

self.lineView = [[UIView alloc]init];

self.lineView.backgroundColor = [UIColor whiteColor];

self.lineView.height = height;

self.lineView.width = titleBtn.titleLabel.width;

self.lineView.top = y; // top 就是y值

self.lineView.centerX = titleBtn.centerX;

self.lineView.backgroundColor = [UIColor whiteColor];

[self addSubview:self.lineView];

}

}

}

return self;

}

//MainVc 滚动时调用

-(void)scrolling:(NSInteger)tag{

//根据tag 拿到btn

UIButton * button = self.btns[tag];

[UIView animateWithDuration:0.25 animations:^{

self.lineView.centerX = button.centerX;

}];

}

//点击事件

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

self.block(button.tag);

[self scrolling:button.tag];

}


MainVC.h

#import "RZKMainViewController.h"

#import "RZKMainTopView.h"

@interface RZKMainViewController ()@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;

@property(nonatomic,strong)NSArray * datalist;

@property(nonatomic,strong)RZKMainTopView * topview;

@end

@implementation RZKMainViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self iniUI];

}

-(void)iniUI{

//添加子视图控制器

[self setupChildViewControllers];

//加载导航栏按钮

[self setupNav];

//    self.view.backgroundColor = [UIColor whiteColor];

}

//导航栏左右按钮

-(void)setupNav{

//topview

self.navigationItem.titleView = self.topview;

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"global_search"] style:UIBarButtonItemStyleDone target:nil action:nil];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"title_button_more"] style:UIBarButtonItemStyleDone target:nil action:nil];

}

//子视图控制器

-(void)setupChildViewControllers{

NSArray * vcNames =@[@"RZKFocuseViewController",@"RZKHotViewController",@"RZKNearViewController"];

for (NSInteger i = 0; i < vcNames.count; i++) {

NSString * vcName = vcNames[i];

UIViewController * vc = [[NSClassFromString(vcName) alloc]init];

vc.title = self.datalist[i];

[self addChildViewController:vc];

}

//将子控制器的View 添加到mainVc 的ScrollerView上

//设置scrollView的ContentSize偏移量

self.contentScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.datalist.count,0);

//进入MainView 后 直接跳转到第二个界面

self.contentScrollView.contentOffset= CGPointMake(SCREEN_WIDTH, 0);

self.contentScrollView.pagingEnabled = YES;

self.contentScrollView.delegate = self;

//进入主控制器 加载第一个界面

[self scrollViewDidEndDecelerating:self.contentScrollView];

}

#pragma mark -- 滚动视图动画结束调动代理方法

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

//得到每一个自控制器View 的宽度

CGFloat width = SCREEN_WIDTH;

CGFloat height = SCREEN_HEIGHT;

CGFloat offset = scrollView.contentOffset.x;

//获取当前控制器的索引

NSInteger index = offset/width;

//索引值联动 topview

[self.topview scrolling:index];

//根据索引值返回vc的引用

UIViewController * vc = self.childViewControllers[index];

//判断当前的Vc是否执行过ViewDidLoad

if ([vc isViewLoaded])return;

//设置子控制器的大小

vc.view.frame = CGRectMake(offset,0,width,height);

//将当前的VC的View 添加到ScrollView 上

[scrollView addSubview:vc.view];

}

#pragma mark -- 滚动视图减速代理方法

//减速结束 调用 加载子视图 控制器view 的方法

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

//直接调用滚动视图动画结束调动代理方法

[self scrollViewDidEndScrollingAnimation:scrollView];

/**

* 逻辑是 当前的view 加载过了就不添加了

* 没有添加过 就要进行添加

**/

}

-(RZKMainTopView *)topview{

if (!_topview) {

_topview = [[RZKMainTopView alloc]initWithFrame:CGRectMake(0, 0, 200, 44) titleNames:self.datalist tapView:^(NSInteger tag) {

CGPoint point = CGPointMake(tag * SCREEN_WIDTH,self.contentScrollView.contentOffset.y);

[self.contentScrollView setContentOffset:point animated:YES];

}];

}

return _topview;

}

-(NSArray *)datalist{

if (!_datalist ) {

_datalist = @[@"关注",@"热门",@"附近"];

}

return _datalist;

}

你可能感兴趣的:(iOS 顶部视图家滚动视图联动)