iOS-图片无限滚动控件

因为项目需要和多方原因,使用UICollectionView封装的一个实现图片无限滚动的Demo,利用代理实现点击事件监听,可以设置滚动时间间隔(在小码哥的代码基础上改写的)。github地址

@protocol TYCOViewsDelegate <NSObject>
@optional
//点击的代理方法
-(void)TYCOViewClickedImageAtItem:(NSInteger)item;
@end

//加载的本地图片
@property (nonatomic,strong) NSArray *images;
//加载的网络图片
@property (nonatomic,strong) NSArray *SD_images;
//占位图片
@property (nonatomic,strong) UIImage *bitmapImage;
//滚动时间间隔
@property (nonatomic,assign) CGFloat CO_Interval;

 

根据需要在这里调整PageControl的frame

@implementation TYCOViews

-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame collectionViewLayout:[self createLayoutWithFrame:frame]]) {

_pageControl = [[UIPageControl alloc] init];
//根据需要在这里调整pagecontrol的位置
_pageControl.frame = CGRectMake((TYCO_Width-20 * _pageControl.numberOfPages)*0.5, TYCO_Height, 20 * _pageControl.numberOfPages, 20);

 创建方法:

_TYCOView = [[TYCOViews alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenW*0.5)];
_TYCOView.CO_Interval = 2;
_TYCOView.images = _viewModel.imageArray;
[self addSubview:_TYCOView];

 

详细代码展示:

#import "TYCOViews.h"
#import "TYCOCell.h"

// 每一组最大的行数
#define TYCOTotalRowsInSection (5000 * self.images.count)
#define TYCODefaultRow (NSUInteger)(TYCOTotalRowsInSection * 0.5)

#define TYCO_Width self.frame.size.width
#define TYCO_Height self.frame.size.height-30

@interface TYCOViews ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,retain) UIPageControl *pageControl;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,assign) BOOL isSDWebImage;
@end

@implementation TYCOViews

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame collectionViewLayout:[self createLayoutWithFrame:frame]]) {
        
        _pageControl = [[UIPageControl alloc] init];
        //根据需要在这里调整pagecontrol的位置
        _pageControl.frame = CGRectMake((TYCO_Width-20 * _pageControl.numberOfPages)*0.5, TYCO_Height, 20 * _pageControl.numberOfPages, 20);
        _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.delegate = self;
        self.dataSource = self;
        self.bounces = NO;
        self.backgroundColor = [UIColor whiteColor];
        _isSDWebImage = NO;
    }
    return self;
}

-(UICollectionViewLayout *)createLayoutWithFrame:(CGRect)frame{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.minimumLineSpacing = 0;
    layout.itemSize = frame.size;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    return layout;
}
-(void)setCO_Interval:(CGFloat)CO_Interval{
    _CO_Interval = CO_Interval;
    [self addTimer];
}
- (void)removeTimer{
    [self.timer invalidate];
    self.timer = nil;
}
#pragma mark -- 添加定时器
- (void)addTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:_CO_Interval target:self selector:@selector(nextNews) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
-(void)nextNews{
    NSIndexPath *visiablePath = [[self indexPathsForVisibleItems] firstObject];
    NSUInteger visiableItem = visiablePath.item;
    if ((visiableItem % self.images.count) == 0) {
        [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:TYCODefaultRow inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        visiableItem = TYCODefaultRow;
    }
        NSUInteger nextItem = visiableItem + 1;
        [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
#pragma mark -- 设置图片资源
-(void)setImages:(NSArray *)images{
    _images = images;
    _pageControl.numberOfPages = self.images.count;
    [self reloadData];
    [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:TYCODefaultRow inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    //解决复用时pageControl的短暂消失重现
    dispatch_async(dispatch_get_main_queue(), ^{
        [self addpageControl];
    });
}
#pragma mark -- 这是网络图片
-(void)setSD_images:(NSArray *)SD_images{
    _isSDWebImage = YES;
    [self setImages:SD_images];
}
#pragma mark -- 添加pageControl
-(void)addpageControl{
    [[self superview] addSubview:_pageControl];
}
#pragma mark -- 数据源

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return TYCOTotalRowsInSection;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TYCOCell *cell = [TYCOCell TYCOCellWithCollectionView:collectionView indexPath:indexPath];
    if (!_isSDWebImage) {
        NSDictionary *dic = self.images[indexPath.item % self.images.count];
        cell.icon = dic[@"icon"];
    }else{
        NSString *iconUrl = self.images[indexPath.item % self.images.count];
        cell.iconUrl = iconUrl;
    }
//    [cell addSubview:_pageControl];
    cell.bitmapImage = self.bitmapImage;
    return cell;
}
#pragma mark -- 滑动
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger index = indexPath.item % self.images.count;
    _pageControl.currentPage = index;
}

//手势拖拽
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeTimer];
}

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    [self addTimer];
}

//点击的方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if ([self.TYDelegate respondsToSelector:@selector(TYCOViewClickedImageAtItem:)]) {
        [self.TYDelegate TYCOViewClickedImageAtItem:self.pageControl.currentPage];
    }
}

自定义cell

#import <UIKit/UIKit.h>

@interface TYCOCell : UICollectionViewCell
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *iconUrl;
@property (nonatomic,strong) UIImage *bitmapImage;

+(instancetype)TYCOCellWithCollectionView:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath;

@end
#import "TYCOCell.h"
@interface TYCOCell()
@property(strong,nonatomic) UIImageView *imageView;
@end
@implementation TYCOCell
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self.contentView addSubview:self.imageView];
    }
    return self;
}
+(instancetype)TYCOCellWithCollectionView:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"TYCOCell";
    [collectionView registerClass:[TYCOCell class] forCellWithReuseIdentifier:cellId];
    TYCOCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    return cell;
}
-(void)setIconUrl:(NSString *)iconUrl{
//    [self.imageView sd_setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:_bitmapImage];
}

-(void)setIcon:(NSString *)icon{
    UIImage *image = [UIImage imageNamed:icon];
    self.imageView.image = image;
//    self.backgroundView =
}

@end

 

转载于:https://www.cnblogs.com/gold-silence/p/7245735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值