美文网首页
WKTitleScroll 的使用

WKTitleScroll 的使用

作者: 我太难了_9527 | 来源:发表于2017-08-15 15:46 被阅读0次

实际项目中我们可能经常遇到一个界面有很多的子视图,就如今日头条的那样, 因此在下在这里简单的写了一个 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];
}
}

相关文章

  • WKTitleScroll 的使用

    实际项目中我们可能经常遇到一个界面有很多的子视图,就如今日头条的那样, 因此在下在这里简单的写了一个 demo 来...

  • iconfont的使用(下载使用)

    1、下载文件 2、在生命周期中引入项目 beforeCreate () { var domModule = ...

  • Gson的使用--使用注解

    Gson为了简化序列化和反序列化的过程,提供了很多注解,这些注解大致分为三类,我们一一的介绍一下。 自定义字段的名...

  • 记录使用iframe的使用

    默认记录一下----可以说 这是我第一次使用iframe 之前都没有使用过; 使用方式: 自己开发就用了这几个属...

  • with的使用

    下面例子可以具体说明with如何工作: 运行代码,输出如下

  • this的使用

    什么是this? this是一个关键字,这个关键字总是返回一个对象;简单说,就是返回属性或方法“当前”所在的对象。...

  • this的使用

    JS中this调用有几种情况 一:纯粹的函数调用 这是函数的最通常用法,属于全局性调用,因此this就代表全局对象...

  • ==的使用

    积累日常遇到的编码规范,良好的编码习惯,持续更新。。。 日常使用==用于判断的时候,习惯性将比较值写前面,变量写后...

  • this的使用

    1.默认绑定,就是函数立即执行。 函数立即执行就是指向window,但是如果是node环境,就是指向全局conso...

  • %in% 的使用

    写在前面:From 生信技能书向量难点之一:%in% 难点 (1)== 与 %in% 的区别== 强调位置,x和对...

网友评论

      本文标题:WKTitleScroll 的使用

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