仿新聞客戶端首頁

看了許多仿網(wǎng)易新聞客戶端,騰訊新聞客戶端的demo,都會存在一些bug,與其這找別人demo,還不如自己寫一個方便.廢話不多說,先看一下實現(xiàn)的效果:


在這里我主要說一下實現(xiàn)方法:

頭部標(biāo)題用了一個scrollview來實現(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];

}


下面展示子控件的底部也是一個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];

}

點擊title時

#pragma mark - 標(biāo)題label的點擊事件

- (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居中方法 這個方法相對來說比較重要

- (void)titleLabelToCenter:(UILabel *)selectLabel{

//計算偏移量

CGFloat offsetX = selectLabel.center.x - self.contentScrollView.width * 0.5;

if (offsetX<0) {

offsetX = 0;

}

//獲取最大滾動范圍

CGFloat offsetMax = self.titleScorllView.contentSize.width - self.contentScrollView.width;

if (offsetMax < 0) {

offsetMax = 0;

}

if (offsetX > offsetMax) {

offsetX = offsetMax;

}

//滾動

[self.titleScorllView setContentOffset:CGPointMake(offsetX, 0) animated:YES];

}


代理方法?

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

//計算滑到哪一頁

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];

}

這樣展示讀者估計會蒙圈的,還是直接上demo吧! demo連接是:http://m.itdecent.cn/writer#/notebooks/8035700/notes/8569624

代碼很多不足之處 還請諒解!我也只是小白一個!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容