IOS开发基础之网易新闻UICollectionView的使用第3天

由于第3天的UICollectionView 并不实现,我查阅相关资料,也没解决,先从本地的plist加载的数据,不是网络的上的数据。有一定的参考意义。
父类是UIViewController 通过纯代码添加进去的,addSubView 方式添加进去的,这跟UITableViewController类似,可以拖控件,指定相应的类,但是这种方法并没有学会。源码在我的主页下面。等回头这块知识健全了,再补充回来。

//
//  ViewController.m
//  imageRunLoop1
//
//  Created by 鲁军 on 2021/4/11.
//#import "HMImageLoopController.h"
#import "YYCell.h"
#import "YYNews.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
#define MaxSections 100
@interface HMImageLoopController()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic , strong) UICollectionView *collectionView;
@property (nonatomic , strong) UIPageControl *pageControl;
@property (nonatomic , strong) NSMutableArray *newses;
@property (nonatomic , strong) NSTimer *timer;
@property (nonatomic, strong) NSMutableArray *news;
@end
@implementation HMImageLoopController
-(NSArray *)newses{if (_newses == nil) {NSBundle *bundle = [NSBundle mainBundle];NSString *path =[bundle pathForResource:@"resource.plist" ofType:nil];NSArray *array = [NSArray arrayWithContentsOfFile:path];_newses=[NSMutableArray array];for (NSDictionary *dict in array) {[_newses addObject: [YYNews newsWithDict:dict]];}}return  _newses;
}- (void)viewDidLoad{[super viewDidLoad];[self.view addSubview:self.collectionView];[self.view addSubview:self.pageControl];// 注册cell[self.collectionView registerClass:[YYCell class] forCellWithReuseIdentifier:@"Cell"];// 添加定时器 实现轮播功能呢[self addTimer];}- (void)addTimer
{_timer = [NSTimer scheduledTimerWithTimeInterval:8.0 target:self selector:@selector(nextPage) userInfo:NULL repeats:YES];}
// 定时器的内容
- (void)nextPage
{// 获取当前的 indexPathNSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];NSIndexPath *currentIndexPathSet = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:MaxSections / 2];[self.collectionView scrollToItemAtIndexPath:currentIndexPathSet atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];// 设置下一个滚动的item的indexPathNSInteger nextItem = currentIndexPathSet.item + 1;NSInteger nextSection = currentIndexPathSet.section;if (nextItem == self.news.count) {// 当item等于轮播图的总个数的时候// item等于0, 分区加1// 未达到的时候永远在50分区中nextItem = 0;nextSection ++;}// NSLog(@"----%ld---%ld", nextItem, nextSection);NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];[self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}#pragma mark ----ScrollView 代理方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{// 添加定时器[self addTimer];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{// 移除定时器[self.timer invalidate];self.timer = nil;
}-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{// 滚动时 动态设置 pageControl.currentPage//int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5) % self.news.count;int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5) % 5;self.pageControl.currentPage = page;
}#pragma mark ---- 创建集合视图// 创建集合视图
- (UICollectionView *)collectionView
{if (!_collectionView) {// 创建UICollectionViewFlowLayout约束对象UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];// 设置item的Size大小flowLayout.itemSize = CGSizeMake(kWidth, kHeight);// 设置uicollection 的 横向滑动flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;flowLayout.minimumLineSpacing = 0;_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:flowLayout];// 设置代理_collectionView.delegate = self;_collectionView.dataSource = self;// 设置不展示滑动条_collectionView.showsHorizontalScrollIndicator = NO;// 设置整页滑动_collectionView.pagingEnabled = YES;_collectionView.backgroundColor = [UIColor clearColor];// 设置当前collectionView 到哪个位置(indexPath row 0 section 取中间(50个))[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:MaxSections / 2] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];}return _collectionView;}- (UIPageControl *)pageControl
{if (!_pageControl) {UIPageControl *pageControl = [[UIPageControl alloc] init];pageControl.center = CGPointMake(kWidth / 2, kHeight - 100);pageControl.numberOfPages = _news.count;pageControl.bounds = CGRectMake(0, 0, 150, 40);pageControl.enabled = NO;pageControl.pageIndicatorTintColor = [UIColor blueColor];pageControl.currentPageIndicatorTintColor = [UIColor redColor];[self.view addSubview:pageControl];_pageControl = pageControl;}return _pageControl;
}
#pragma mark --- 数据源
- (NSMutableArray *)news
{if (!_news) {NSString *path = [[NSBundle mainBundle] pathForResource:@"resource.plist" ofType:nil];NSArray *arr = [NSArray arrayWithContentsOfFile:path];_news = [NSMutableArray array];for (NSDictionary *dic1 in arr) {[_news addObject:[YYNews newsWithDict:dic1]];}}return _news;
}
/**[HMHeadline headlines:^(NSArray * _Nonnull array) {self.headlines = array;} errorBlock:^(NSError * _Nonnull err) {NSLog(@"出错了");}];*/#pragma mark --- 实现collectionView代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{return MaxSections;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{return self.news.count;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{static NSString *string = @"Cell";YYCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath];if (!cell) {cell = [[YYCell alloc] init];}cell.news = self.news[indexPath.row];return cell;
}
@end
//
//  YYCell.m
//  图片轮播(无限循环)
//
//  Created by yixiang on 14/12/12.
//  Copyright (c) 2014年 yixiang. All rights reserved.
//#import "YYCell.h"
#import "YYNews.h"@interface YYCell()
@property (weak , nonatomic)  UILabel *label;
@property (weak , nonatomic)  UIImageView *imageView;
@end
@implementation YYCell-(instancetype)initWithFrame:(CGRect)frame{self =[super initWithFrame:frame];if (self) {UIImageView *img = [[UIImageView alloc] init];[self.contentView addSubview:img];self.imageView = img;UILabel *lab = [[UILabel alloc] init];[self.contentView addSubview:lab];self.label = lab;}return self;
}-(void)setNews:(YYNews *)news
{_news=news;[self settingData];[self settingFrame];
}#pragma mark 给子控件赋值
-(void) settingData{self.imageView.image = [UIImage imageNamed:_news.icon];self.label.text = _news.title;}#pragma mark 设置子控件的frame
-(void) settingFrame{CGFloat screenWidth = self.contentView.frame.size.width;self.imageView.frame = CGRectMake(0, 0, screenWidth, 200);self.label.frame = CGRectMake(0, 0, screenWidth, 200);self.label.font = [UIFont systemFontOfSize:30];self.label.textAlignment = NSTextAlignmentCenter;
}@end
#import <UIKit/UIKit.h>
@class YYNews;@interface YYCell : UICollectionViewCell
@property (nonatomic, strong) YYNews *news;
@end
#import <Foundation/Foundation.h>@interface YYNews : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *icon;-(id)initWithDict:(NSDictionary *)dict;
+(id)newsWithDict : (NSDictionary *) dict;
@end
#import "YYNews.h"@implementation YYNews-(id)initWithDict:(NSDictionary *)dict{if (self=[super init]) {self.title = dict[@"title"];self.icon = dict[@"icon"];}return self;
}+(id)newsWithDict:(NSDictionary *)dict{return [[self alloc] initWithDict:dict];
}
@end

IOS开发基础之网易新闻UICollectionView的使用第3天相关推荐

  1. IOS开发基础之网易新闻JSON转模型数组第2天

    IOS开发基础之网易新闻JSON转模型数组第2天 // // HMHeadline.h // 01-网易新闻搭建 // // Created by 鲁军 on 2021/4/11. //#import ...

  2. IOS开发基础之网易新闻环境搭建异步请求json,AFN网络封装第1天

    IOS开发基础之网易新闻环境搭建异步请求json,AFN网络封装第1天 视频资料是2015年的,但是AFN是导入框架的关键文件,我尝试使用cocoapods安装最新的AFN,虽然成功了,但是版本太高, ...

  3. IOS开发简易的网易新闻页面

    IOS开发简易的网易新闻页面 这是简易的网易新闻,使用swift语法去写的 git 仓库地址: https://e.coding.net/lujun1/wangyijianyixinwenswiftb ...

  4. Flutter开发之《网易新闻客户端Flutter混合开发实践》笔记(52)

    摘自:网易新闻客户端Flutter混合开发实践 引言 网易新闻项目本身很庞大,业务繁多,全部改为Flutter实现肯定是不现实的,在使用Flutter的前期阶段,我们挑选了相对独立的几个模块,在现有工 ...

  5. iOS开发基础知识--碎片44

    iOS开发基础知识--碎片44  iOS开发基础知识--碎片44 1:App跳转至系统Settings 跳转在IOS8以上跟以下是有区别的,如果是IOS8以上可以如下设置: NSURL *url = ...

  6. iOS开发基础-九宫格坐标(4)

    对iOS开发基础-九宫格坐标(3)的代码进行进一步优化. 新建一个 UIView 的子类,并命名为 WJQAppView ,将 appxib.xib 中的 UIView 对象与新建的视图类进行关联. ...

  7. IOS开发基础之OC的Block入门_Day09-Block

    IOS开发基础之OC的Block入门_Day09-Block block是oc的重要的基础知识,重点之重.跟协议一样重要,是进行函数回调重要手段.在后续的UI学习具有举足轻重的地位.学会基础的bloc ...

  8. IOS开发基础之音频工具类封装AVAudioPlayer

    IOS开发基础之音频工具类封装AVAudioPlayer 源码在我的主页下面 ,项目名称是AVAudioPlayer 关键性代码 工具类的封装 // // LJAudioTool.h // AVAud ...

  9. IOS开发基础之微博项目第1天-OC版

    IOS开发基础之微博项目第1天-OC版 纯代码创建的项目,具有参考价值 该资料来自2014年7月3号,虽然时间过去较长,但是oc和swift不同,oc语法迭代更新慢 具有一定的参考意义 涉及xib加载 ...

最新文章

  1. Eclipse将android 类编译为jar类库
  2. eclipse中报错:java.lang.OutOfMemoryError: Java heap space
  3. MySQL 8.0 首个自适应参数横空出世
  4. zookeeper是做什么用的_做橱柜用什么门板好 选对很关键
  5. Linux系统编程学习问题回顾
  6. Java 高级特性 --- 反射
  7. DreamFacotry 第4章 如何保护REST API
  8. Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建
  9. [UE4]Size Box
  10. VS C++ def
  11. Python源码剖析笔记3-Python执行原理初探
  12. Epub,Mobi,Azw3电子书格式的区别,有什么好用的IOS手机epub阅读器
  13. 计算机软著发明,时健
  14. 竹笛的分类有哪些?来认识竹笛的大家族。
  15. 湖北移动B863AV3.1-M2_S905L3A_UWE5621DS_安卓9语音线刷包--支持语音-首页正常-设置不要密码-灯正
  16. 青少年计算机编程少儿编程小学生编程是否适合
  17. 打造中国版《头号玩家》?《夺命玩家》元宇宙概念小说走火
  18. Ubuntu USB设备端口号绑定
  19. 【C语言】函数基本知识详细讲解(库函数、自定义函数、参数、函数的套用.....
  20. CF1514C---数论

热门文章

  1. curl命令java_让 Bug 无处藏身,Java 线上问题排查思路、常用工具
  2. 用博奥如何导入单项工程电子表_博奥工程系列软件实操手册(第六册).doc
  3. python线下课程厦门_厦门python课程
  4. access游戏库不显示 ea_英伟达上线云游戏服务 千款游戏月费4.99美元
  5. php导入关系表,PHP导入Execl表到数据库
  6. mysql索引底层图_MySQL索引底层数据结构
  7. python代理池_用Python搭建一个简单的代理池
  8. 文字抖动_如何用PS制作故障风文字效果
  9. java邮件中添加excel_使用java api 创建excel内容并发送邮件
  10. linux usb ga驱动详解,Linux设备驱动之内存映射