关闭

用UICollectionView实现无限轮播案列

1221人阅读 评论(0) 收藏 举报
分类:

1.自定义View

//
//  BannerView.h

#import <UIKit/UIKit.h>

@interface BannerView : UIView

/**
 *  实例化bannerView视图
 */
+ (instancetype)bannerView;

/**
 *  用来接收控制器传递的数据
 */
@property(nonatomic,strong) NSArray *banners;
@end

//
//  BannerView.m

#import "BannerView.h"
#import "BannerCell.h"

// 每一组最大的行数
#define TotalRowsInSection (5000 * self.banners.count)
#define DefaultRow (NSUInteger)(TotalRowsInSection * 0.5)

@interface BannerView() <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong, nonatomic) NSTimer *timer;
@end
@implementation BannerView

+ (instancetype)bannerView
{
    //加载xib
    return [[[NSBundle mainBundle] loadNibNamed:@"BannerView" owner:nil options:nil]firstObject];
}

/**
 *  加载.nib会调用这个方法
 */
- (void)awakeFromNib
{
    //UICollectionViewCell重用cell必须注册
    [self.collectionView registerNib:[UINib nibWithNibName:@"BannerCell" bundle:nil] forCellWithReuseIdentifier:@"bannerCell"];
    
    //添加定时器
    [self addTimer];
  
}

- (void)setBanners:(NSArray *)banners
{
    _banners = banners;
    
    //1.设置pageControl的总页数
    self.pageControl.numberOfPages = self.banners.count;
    
    //2.刷新数据
    [self.collectionView reloadData];
    
    //3. 默认组
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:DefaultRow inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}


/**
 *  移除定时器
 */
- (void)removeTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

/**
 *  添加定时器
 */
- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(next) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

/**
 *  下一页
 */
- (void)next
{
    NSIndexPath *visiablePath = [[self.collectionView indexPathsForVisibleItems] firstObject];
    
    NSUInteger visiableItem = visiablePath.item;
    if ((visiablePath.item % self.banners.count)  == 0) { // 第0张图片
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:DefaultRow inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        visiableItem = DefaultRow ;
    }
    
    NSUInteger nextItem = visiableItem + 1;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];

}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return TotalRowsInSection;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"bannerCell";
    BannerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.model = self.banners[indexPath.item % self.banners.count];
    return cell;
}

#pragma mark - UICollectionViewDelegate
/**
 *  cell显示结束之后调用(滑动了一页)
 */
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *visiablePath = [[collectionView indexPathsForVisibleItems] firstObject];
    //设置pageControl的当前页
    self.pageControl.currentPage = visiablePath.item % self.banners.count;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self addTimer];
}

@end


2.自定义cell

//
//  BannerCell.h


#import <UIKit/UIKit.h>

@class BannerModel;
@interface BannerCell : UICollectionViewCell
/**
 *  接收banner模型数据,用于显示到cell上
 */
@property (strong, nonatomic) BannerModel *model;
@end

//
//  BannerCell.m


#import "BannerCell.h"
#import "BannerModel.h"

@interface BannerCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;

@end
@implementation BannerCell

- (void)awakeFromNib {
    // Initialization code
}

/**
 *  重写model属性,显示数据到视图上
 */
- (void)setModel:(BannerModel *)model
{
    _model = model;
    
    self.iconView.image = [UIImage imageNamed:model.icon];
}
@end

3.用到的模型

//
//  BannerModel.h

#import <Foundation/Foundation.h>

@interface BannerModel : NSObject
@property (copy, nonatomic) NSString *icon;

/** 用字典实例化对象的成员方法 */
- (instancetype)initWithDict:(NSDictionary *)dict;
/** 用字典实例化对象的类方法,又称工厂方法 */
+ (instancetype)bannerWithDict:(NSDictionary *)dict;
@end
//
//  BannerModel.m


#import "BannerModel.h"

@implementation BannerModel


/** 用字典实例化对象的成员方法 */
- (instancetype)initWithDict:(NSDictionary *)dict
{
    self = [self init];
    if (self) {
        self.icon = dict[@"icon"];
    }
    return self;
}
/** 用字典实例化对象的类方法,又称工厂方法 */
+ (instancetype)bannerWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

@end

4.控制器使用

#import "VideoViewController.h"
#import "BannerView.h"
#import "BannerModel.h"

@interface VideoViewController ()
@property(nonatomic,strong)NSArray *banners;
@end

@implementation VideoViewController

/**
 *  懒加载 创建banner的数据模型
 */
- (NSArray *)banners
{
    if (!_banners) {
        //1.加载plist文件
        NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"newses.plist" ofType:nil]];
        
        
        NSMutableArray *arrayM = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            [arrayM addObject:[BannerModel bannerWithDict:dict]];
        }
        
        _banners = arrayM;

    }
    return _banners;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    //self.automaticallyAdjustsScrollViewInsets = NO;
    
    //创建bannerView
    BannerView *bannerView = [BannerView bannerView];
    bannerView.banners = self.banners;
    [self.view addSubview:bannerView];
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1303600次
    • 积分:23651
    • 等级:
    • 排名:第301名
    • 原创:1234篇
    • 转载:156篇
    • 译文:0篇
    • 评论:125条
    最新评论