簡易類網易滑動

? ? 有很多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的不用添加代理

我是代碼鏈接

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容