实际项目中我们可能经常遇到一个界面有很多的子视图,就如今日头条的那样, 因此在下在这里简单的写了一个 demo 来解决, 并附上github 地址https://github.com/wyxlh/WKTitleScorll
- 1,导入头文件 并定义 并实现 UIScrollViewDelegate协议 还需要一些宏定义,具体的看
//获取设备的物理高度
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
//获取设备的物理宽度
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
#define SKOrangeColor [UIColor colorWithRed:250/255. green:50/255. blue:100/255. alpha:1]
#import "TitleScrollView.h"
#import "UIView+Frame.h"
@property (nonatomic,strong)TitleScrollView *titleScroll;
- 2 实现
- (TitleScrollView *)titleScroll
{
if (!_titleScroll)
{
WS(weakSelf)
_titleScroll = [[TitleScrollView alloc] initWithFrame:CGRectMake(0,64, ScreenWidth, 47) TitleArray:self.titleArr selectedIndex:0 scrollEnable:NO lineEqualWidth:YES isLarger:YES selectColor:SKOrangeColor defaultColor:[UIColor blackColor] SelectBlock:^(NSInteger index) {
[weakSelf titleClick:index];
}];
_titleScroll.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_titleScroll];
}
return _titleScroll;
}
- 3添加子视图 并 实现对应的协议
#pragma mark 底部的scrollview
-(void)setupContentView {
//不要自动调整inset
self.automaticallyAdjustsScrollViewInsets = NO;
UIScrollView *contentView = [[UIScrollView alloc] init];
contentView.frame = CGRectMake(0, self.titleScroll.height, ScreenWidth, ScreenHeight);
contentView.delegate = self;
contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
contentView.pagingEnabled = YES;
[self.view insertSubview:contentView atIndex:0];
self.contentView = contentView;
self.contentView.contentOffset = CGPointMake(0*ScreenWidth, 0);
//添加第一个控制器的view
[self scrollViewDidEndScrollingAnimation:contentView];
}
#pragma mark 便签栏按钮点击
-(void)titleClick:(NSInteger)index {
//滚动,切换子控制器
CGPoint offset = self.contentView.contentOffset;
offset.x = index * self.contentView.width;
[self.contentView setContentOffset:offset animated:YES];
}
#pragma mark -UIScrollViewDelegate
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
//添加子控制器的view
//当前索引
NSInteger index = scrollView.contentOffset.x / scrollView.width;
//取出子控制器
UIViewController *vc = self.childViewControllers[index];
vc.view.x = scrollView.contentOffset.x;
vc.view.y = 0;//设置控制器的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;
//点击butto
[self.titleScroll setSelectedIndex:index];
}
-(void)addChildViewControllers{
for (int i = 0; i < self.titleArr.count; i++) {
WKChildViewController *child = [[WKChildViewController alloc]init];
child.index = i;
[self addChildViewController:child];
}
}