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
{
// 获取当前的 indexPath
NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
NSIndexPath *currentIndexPathSet = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:MaxSections / 2];
[self.collectionView scrollToItemAtIndexPath:currentIndexPathSet atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
// 设置下一个滚动的item的indexPath
NSInteger 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