iOS 类似网易新闻界面

1.gif

大家好,我又回来了。哥不是老司机,但是车也开得不错。做过项目的都遇到过这样的界面,反正我是感觉难不难,就是需要的逻辑比较多。(就是墨迹)而且现在这样的界面许多APP都用,也比较火吧!好了,废话不多说,进入正题。开车了,上车的乘客坐稳了!!!!(代码已经修改!!!) 下面有的代码可以忽略了。谢谢,感兴趣的下面有链接。
ViewController.m

#import "ViewController.h"
#import "CellOfFirst.h"// 自定义的cell
#import "CellOfSmall.h"// 自定的cell
#define WIDTH self.view.frame.size.width
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
// 一些需要的属性
@property (nonatomic,strong) UICollectionView *collection;
@property (nonatomic, strong) UICollectionView *smallOfCollection;
@property (nonatomic, assign) NSInteger labelIndex;// 记录是第几个label
@property (nonatomic, strong) NSIndexPath *oldIndexPath;//记录第几个label的indexpath
@property (nonatomic,strong) UIView *redView;//小红条
@property (nonatomic,strong)         NSMutableArray *marr ;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.marr = [NSMutableArray arrayWithObjects:@"新闻",@"视频",@"音乐",@"兴趣",@"呵呵",@"哈哈",@"嘻嘻",@"嘿嘿", @"司机",@"开车",nil];
    [self config];
    [self createRedView];
    [self createKVOAction];
    // Do any additional setup after loading the view, typically from a nib.
}

首先我们在VC中铺两个collectionView,一个大的、一个小的,这个大家一定都会,就不详细说了!

- (void)config {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 55);
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 55, self.view.frame.size.width, self.view.frame.size.height - 55) collectionViewLayout:layout];
    [self.view addSubview:self.collection];
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.showsHorizontalScrollIndicator = NO;
    self.collection.pagingEnabled = YES;
    [self.collection registerClass:[CellOfFirst class] forCellWithReuseIdentifier:@"pool"];
    
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc]init];
    layout1.itemSize = CGSizeMake(self.view.frame.size.width / 5, 50);
    layout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout1.minimumLineSpacing = 0;
    self.smallOfCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 5, self.view.frame.size.width, 50) collectionViewLayout:layout1];
    [self.view addSubview:self.smallOfCollection];
    self.smallOfCollection.backgroundColor = [UIColor whiteColor];
    self.smallOfCollection.delegate = self;
    self.smallOfCollection.dataSource = self;
    [self.smallOfCollection registerClass:[CellOfSmall class] forCellWithReuseIdentifier:@"smallPool"];
  // 关闭横向的指示器  self.smallOfCollection.showsHorizontalScrollIndicator = NO;
}

collectionView 的两个协议方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.collection) {
        return _marr.count;
    }
    else {
        return _marr.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collection) {
        CellOfFirst *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1.0];
        return cell;
    }
    else  {
        CellOfSmall *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"smallPool" forIndexPath:indexPath];
       
        cell.label.text = _marr[indexPath.item];
        cell.label.textAlignment = 1;//字体居中
        //保证开启程序第一个字是红色的 并且字体为大字
        if (indexPath.row == _labelIndex) {
            cell.label.textColor = [UIColor redColor];
            cell.label.font = [UIFont systemFontOfSize:18];
        }else {
            cell.label.font = [UIFont systemFontOfSize:15];
            cell.label.textColor = [UIColor blackColor];
        }
        if (indexPath.row == _oldIndexPath.row) {
            cell.label.textColor = [UIColor redColor];
            cell.label.font = [UIFont systemFontOfSize:18];
        }else {
            cell.label.font = [UIFont systemFontOfSize:15];
            cell.label.textColor = [UIColor blackColor];

        }
        
        
        //cell.backgroundColor = [UIColor redColor];
        return cell;
    }
}

创建小红条

- (void)createRedView {
    self.redView =[[UIView alloc]initWithFrame:CGRectMake(0, 53, self.view.frame.size.width / 5, 2)];
    [self.view addSubview:_redView];
    _redView.backgroundColor = [UIColor redColor];
}

我们来利用KVO模式 ,让VC来观察大collection的动作。

#pragma mark -------添加观察者
- (void)createKVOAction {
    [self.collection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"nil"];
}
// 添加观察者之后 自动调用此方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    CGFloat x = [[change objectForKey:@"new"] CGPointValue].x;
    CGFloat redX = x / 5;
// 小于屏幕宽度时  我们让小红条动
    if (x <= WIDTH) {
        self.redView.transform = CGAffineTransformMakeTranslation(redX, 0);
        
    }
// 最后几个 我们还让小红条动
    else if (x > 6 *WIDTH) {
        self.redView.transform = CGAffineTransformMakeTranslation((redX - 6 * WIDTH / 5) + WIDTH / 5, 0);
        
    }
// 大于屏幕的宽度  让小collectionView动  并且我们不能让他一直动  可以看else if
    else {
        self.smallOfCollection.contentOffset = CGPointMake(redX - WIDTH / 5, 0);
    }
    
}

我们滑动大collectionView 是,调动此方法

// 当滑动已经减速时进行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.collection) {
        // 当前大collectionView 第几个item
        NSInteger currentItemCount = scrollView.contentOffset.x / WIDTH;
        // 根据第几个item获取indexPath
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:currentItemCount inSection:0];
        // 根据indexPath 获取当前的小collectionViewCell
        // 进行强转
        CellOfSmall *currentCell = (CellOfSmall *)[_smallOfCollection cellForItemAtIndexPath:indexPath];
        // 然后将字体变红
        currentCell.label.textColor = [UIColor redColor];
        currentCell.label.font = [UIFont systemFontOfSize:18];
        /** 恢复字体颜色*/
        // 根据之前,再取出cell协议的方法中记录的item下标
        NSIndexPath *indexPathPast = [NSIndexPath indexPathForItem:_labelIndex inSection:0];
        NSLog(@"%ld",self.labelIndex);
        
        /** 加判断防止划一点点就松开,  并没有去到新页面的情况,  则文字不变色 */
        if (indexPath != indexPathPast) {
            // 根据indexPath获取当前的小collectionViewCell
            CellOfSmall *pastCell = (CellOfSmall *)[self.smallOfCollection cellForItemAtIndexPath:indexPathPast];
            pastCell.label.textColor = [UIColor blackColor];
            pastCell.label.font = [UIFont systemFontOfSize:15];
        }
        self.labelIndex = currentItemCount;
        self.oldIndexPath = indexPath;
    
    }
    
}

这就实现了 滑动页面的时候上面的小标题 小红条 也跟着动弹了!!!
好了,车不能开的太久,容易翻车。我们下回写一下点击小标题,页面和小滑条也跟着的事件!!大家可以去尝试一下,我感觉这非常常用。我做的这个只是简单的动画,小滑条的宽度是固定的,没有变化的效果。不足,向大家能够受用

有要源代码的小伙伴可以参考:https://gitee.com/jyj15567471591/jyj_african_andy_lau

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,084评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,623评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,450评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,322评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,370评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,274评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,126评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,980评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,414评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,599评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,773评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,470评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,080评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,713评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,852评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,865评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,689评论 2 354

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,098评论 4 62
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,368评论 7 249
  • 总是不自觉的想起她,脑海中总有一个人的位置,挥之不去,却又得不到的折磨你,与她渐行渐远,遥望天边的云彩,哪有那么多...
    messimi阅读 384评论 0 0
  • 最初的模样 我化了装 穿上厚厚的衣裳 谁还能记得我最初的模样 你的泪光 天上的朦胧月光 街上的霓虹闪亮 让我看到了...
    李木只阅读 251评论 1 2
  • 概念 三阴交穴名意指足部的三条阴经中气血物质在本穴交会。本穴物质有脾经提供的湿热之气,有肝经提供的水湿风气,有肾经...
    云简记阅读 1,531评论 0 1