看了許多仿網(wǎng)易新聞客戶端,騰訊新聞客戶端的demo,都會(huì)存在一些bug,與其這找別人demo,還不如自己寫(xiě)一個(gè)方便.廢話不多說(shuō),先看一下實(shí)現(xiàn)的效果:
在這里我主要說(shuō)一下實(shí)現(xiàn)方法:
頭部標(biāo)題用了一個(gè)scrollview來(lái)實(shí)現(xiàn):
#pragma mark - 頭部標(biāo)題視圖
- (void)creatTopTileView{
UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64,kViewWidth, 40)];?
titleScrollView.backgroundColor = [UIColor grayColor];
titleScrollView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:titleScrollView];
self.titleScorllView = titleScrollView;
self.titleScorllView.contentSize = CGSizeMake(self.titleArr.count * kLabelWidth, 40);
[self creatTopTitleLabel];
}
下面展示子控件的底部也是一個(gè)scrollview
- (void)creatContentScrollView{
UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.titleScorllView.bottom, kViewWidth, kViewHeight - 64 - self.titleScorllView.height)];
contentScrollView.backgroundColor = [UIColor whiteColor];
contentScrollView.showsHorizontalScrollIndicator = NO;
contentScrollView.pagingEnabled = YES;
contentScrollView.bounces = NO;
contentScrollView.delegate = self;
[self.view addSubview:contentScrollView];
self.automaticallyAdjustsScrollViewInsets = NO;
self.contentScrollView = contentScrollView;
self.contentScrollView.contentSize = CGSizeMake(kViewWidth * self.titleArr.count, kViewWidth-64-self.titleScorllView.height);
self.contentScrollView.backgroundColor = [UIColor whiteColor];
//添加字控制器
[self addSubViewController];
}
點(diǎn)擊title時(shí)
#pragma mark - 標(biāo)題label的點(diǎn)擊事件
- (void)titleLabelClick:(UIGestureRecognizer *)recognizer{
UILabel *label = (UILabel *)recognizer.view;
for (int i = 0; i < self.titleLabelArr.count; i++) {
UILabel *titlelabel = self.titleLabelArr[i];
if ([label.text isEqualToString:titlelabel.text]) {
titlelabel.textColor = [UIColor redColor];
}else{
titlelabel.textColor = [UIColor whiteColor];
}
}
[self titleLabelToCenter:label]; //是label居中
[self contentScrollViewOffset:label.tag];
}
//label居中方法 這個(gè)方法相對(duì)來(lái)說(shuō)比較重要
- (void)titleLabelToCenter:(UILabel *)selectLabel{
//計(jì)算偏移量
CGFloat offsetX = selectLabel.center.x - self.contentScrollView.width * 0.5;
if (offsetX<0) {
offsetX = 0;
}
//獲取最大滾動(dòng)范圍
CGFloat offsetMax = self.titleScorllView.contentSize.width - self.contentScrollView.width;
if (offsetMax < 0) {
offsetMax = 0;
}
if (offsetX > offsetMax) {
offsetX = offsetMax;
}
//滾動(dòng)
[self.titleScorllView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}
代理方法?
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//計(jì)算滑到哪一頁(yè)
NSInteger index = scrollView.contentOffset.x / scrollView.width;
self.currentPage = index;
for (int i = 0; i < self.titleLabelArr.count; i ++) {
UILabel *label = self.titleLabelArr[i];
if (i == index) {
label.textColor = [UIColor redColor];
[self titleLabelToCenter:label];
}else{
label.textColor = [UIColor whiteColor];
}
}
//顯示控制器的view
[self showVcView:index];
}
這樣展示讀者估計(jì)會(huì)蒙圈的,還是直接上demo吧! demo連接是:http://www.reibang.com/writer#/notebooks/8035700/notes/8569624
代碼很多不足之處 還請(qǐng)諒解!我也只是小白一個(gè)!