有很多App添加了类网易滑动,自己有了一点思路写出demo给大家参考 只是我的拙见。下面思路代码奉上来 。
效果图如下(页面简陋,请大家见谅呢)
思路:
添加两个UIScrollView 一个表示滑动的item 一个表示内容展示视图,然后在滑动的item上添加若干按钮,在内容展示视图添加相应的层面视图,就可以实现一个简易类网易滑动控件了。
代码如下
1、new file一个文件ZQPageViewController 控制器在h文件添加相关 属性:
//title文字大小
@property(nonatomic,assign)CGFloat titleSize;
2、在m文件中添加控件
@interface ZQPageViewController (){
UIScrollView *_tableScroll;//中间的scroller
UIScrollView *_scrollView;//类网易新闻移动栏目
UIView *_titleView;//类网易新闻的下面的红条框框
}
3、设置上面标题栏的scrollView
- (void)setupScrollView
{
_scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0,64, SWidth, _titleHeight)];
_scrollView.backgroundColor=_itemBackgroundColor;
//设置滑动范围
_scrollView.contentSize = CGSizeMake(SWidth, 30);
[self.view addSubview:_scrollView];
_scrollView.showsVerticalScrollIndicator = NO;
//添加点击按钮
for (int i = 0; i < [_titleArray count]; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake( SWidth/_titleArray.count * i, 0, SWidth/[_titleArray count], 30);
[button setTitle:_titleArray[i] forState:UIControlStateNormal];
//默认选中第一个分页
if (i==0) {
button.selected=YES;
}
//设定按钮文字颜色
[button setTitleColor:_titleNormalColor forState:UIControlStateNormal];
//设定文字颜色
[button setTitleColor:_titleSelectColor forState:UIControlStateSelected];
//设置点击事件
[button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
button.tag =i+1;
//文字大小
button.titleLabel.font = [UIFont systemFontOfSize:_titleSize];
[_scrollView addSubview:button];
}
_scrollView.bounces = NO;
//设置提示条目
_titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 27, SWidth/[_titleArray count], 3)];
//背景颜色
_titleView.backgroundColor = [UIColor redColor];
[_scrollView addSubview:_titleView];
}
4、添加内容视图
-(void)addtableScroll{
//添加滑动视图
_tableScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_scrollView.frame), SWidth, SHeight-CGRectGetMaxY(_scrollView.frame))];
_tableScroll.contentSize = CGSizeMake(SWidth*[_titleArray count],SHeight-CGRectGetMaxY(_scrollView.frame) );
[self.view addSubview:_scrollView];
_tableScroll.delegate=self;
_tableScroll.showsVerticalScrollIndicator = NO;
//设置整页滑动
_tableScroll.pagingEnabled=YES;
[self.view addSubview:_tableScroll];
}
5、按钮点击事件
- (void)titleClick:(UIButton *)button
{
//设置滑动动画
[UIView animateWithDuration:0.4 animations:^{
//移动滑块
_titleView.frame = CGRectMake(button.frame.origin.x, 27,SWidth/[_titleArray count], 3);
//移动主视图
_tableScroll.contentOffset=CGPointMake((button.tag-1)*SWidth, 0);
} completion:^(BOOL finished) {
}];
}
6 设置代理实现联动效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//设定滑动主视图时候滑块随主视图滑动
_titleView.frame = CGRectMake(scrollView.contentOffset.x/SWidth*SWidth/[_titleArray count], 27,SWidth/[_titleArray count], 3);
//确定Scroller不为0
if (scrollView.contentOffset.x/SWidth>=0) {
//取出点击的按钮改变其按钮状态
UIButton *button=(UIButton *)[_scrollView viewWithTag:scrollView.contentOffset.x/SWidth+1];
button.selected=YES;
//将其他已经select状态设置为NO
for (int i=1;i<[_titleArray count]+1; i++) {
if (i!=scrollView.contentOffset.x/SWidth+1) {
UIButton *button=(UIButton *)[_scrollView viewWithTag:i];
button.selected=NO;
}
}
}
}
注意事项
1、设定以前一定要把automaticallyAdjustsScrollViewInsets设置为NO
self.automaticallyAdjustsScrollViewInsets=NO;
2.挂上代理只能是内容视图 item的不用添加代理