? ? 有很多App添加了類(lèi)網(wǎng)易滑動(dòng)抛蚁,自己有了一點(diǎn)思路寫(xiě)出demo給大家參考 只是我的拙見(jiàn)舅踪。下面思路代碼奉上來(lái) 茧痒。?
效果圖如下(頁(yè)面簡(jiǎn)陋,請(qǐng)大家見(jiàn)諒呢)
思路:
? 添加兩個(gè)UIScrollView 一個(gè)表示滑動(dòng)的item 一個(gè)表示內(nèi)容展示視圖锐想,然后在滑動(dòng)的item上添加若干按鈕帮寻,在內(nèi)容展示視圖添加相應(yīng)的層面視圖,就可以實(shí)現(xiàn)一個(gè)簡(jiǎn)易類(lèi)網(wǎng)易滑動(dòng)控件了赠摇。
代碼如下
1固逗、new file一個(gè)文件ZQPageViewController 控制器在h文件添加相關(guān) 屬性:
//title文字大小
@property(nonatomic,assign)CGFloat titleSize;
2、在m文件中添加控件
@interface ZQPageViewController (){
UIScrollView *_tableScroll;//中間的scroller
UIScrollView *_scrollView;//類(lèi)網(wǎng)易新聞移動(dòng)欄目
UIView *_titleView;//類(lèi)網(wǎng)易新聞的下面的紅條框框
}
3藕帜、設(shè)置上面標(biāo)題欄的scrollView
- (void)setupScrollView
{
_scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0,64, SWidth, _titleHeight)];
_scrollView.backgroundColor=_itemBackgroundColor;
//設(shè)置滑動(dòng)范圍
_scrollView.contentSize = CGSizeMake(SWidth, 30);
[self.view addSubview:_scrollView];
_scrollView.showsVerticalScrollIndicator = NO;
//添加點(diǎn)擊按鈕
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];
//默認(rèn)選中第一個(gè)分頁(yè)
if (i==0) {
button.selected=YES;
}
//設(shè)定按鈕文字顏色
[button setTitleColor:_titleNormalColor forState:UIControlStateNormal];
//設(shè)定文字顏色
[button setTitleColor:_titleSelectColor forState:UIControlStateSelected];
//設(shè)置點(diǎn)擊事件
[button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
button.tag =i+1;
//文字大小
button.titleLabel.font = [UIFont systemFontOfSize:_titleSize];
[_scrollView addSubview:button];
}
_scrollView.bounces = NO;
//設(shè)置提示條目
_titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 27, SWidth/[_titleArray count], 3)];
//背景顏色
_titleView.backgroundColor = [UIColor redColor];
[_scrollView addSubview:_titleView];
}
4烫罩、添加內(nèi)容視圖
-(void)addtableScroll{
//添加滑動(dòng)視圖
_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;
//設(shè)置整頁(yè)滑動(dòng)
_tableScroll.pagingEnabled=YES;
[self.view addSubview:_tableScroll];
}
5、按鈕點(diǎn)擊事件
- (void)titleClick:(UIButton *)button
{
//設(shè)置滑動(dòng)動(dòng)畫(huà)
[UIView animateWithDuration:0.4 animations:^{
//移動(dòng)滑塊
_titleView.frame = CGRectMake(button.frame.origin.x, 27,SWidth/[_titleArray count], 3);
//移動(dòng)主視圖
_tableScroll.contentOffset=CGPointMake((button.tag-1)*SWidth, 0);
} completion:^(BOOL finished) {
}];
}
6 設(shè)置代理實(shí)現(xiàn)聯(lián)動(dòng)效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//設(shè)定滑動(dòng)主視圖時(shí)候滑塊隨主視圖滑動(dòng)
_titleView.frame = CGRectMake(scrollView.contentOffset.x/SWidth*SWidth/[_titleArray count], 27,SWidth/[_titleArray count], 3);
//確定Scroller不為0
if (scrollView.contentOffset.x/SWidth>=0) {
//取出點(diǎn)擊的按鈕改變其按鈕狀態(tài)
UIButton *button=(UIButton *)[_scrollView viewWithTag:scrollView.contentOffset.x/SWidth+1];
button.selected=YES;
//將其他已經(jīng)select狀態(tài)設(shè)置為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;
}
}
}
}
注意事項(xiàng)
1洽故、設(shè)定以前一定要把a(bǔ)utomaticallyAdjustsScrollViewInsets設(shè)置為NO
self.automaticallyAdjustsScrollViewInsets=NO;
2.掛上代理只能是內(nèi)容視圖 item的不用添加代理