美文网首页
仿新闻客户端首页

仿新闻客户端首页

作者: 薰衣草儿 | 来源:发表于2017-01-19 16:41 被阅读64次

看了许多仿网易新闻客户端,腾讯新闻客户端的demo,都会存在一些bug,与其这找别人demo,还不如自己写一个方便.废话不多说,先看一下实现的效果:

在这里我主要说一下实现方法:

头部标题用了一个scrollview来实现:

#pragma mark - 头部标题视图

- (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 - 标题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://www.jianshu.com/writer#/notebooks/8035700/notes/8569624

代码很多不足之处 还请谅解!我也只是小白一个!

相关文章

网友评论

      本文标题:仿新闻客户端首页

      本文链接:https://www.haomeiwen.com/subject/aauibttx.html