iOS开发之collectionView实现无限轮播视图
来源:互联网 发布:mac pro是什么 编辑:程序博客网 时间:2024/04/29 09:59
#import <UIKit/UIKit.h>static NSString *ID = @"bannerCell";@class BannerView;@protocol BannerViewDelegate <NSObject>@optional- (void)bannerView:(BannerView *)bannerView didSelectedAtIndex:(NSInteger)nIndex;@end@interface BannerView : UIView@property(nonatomic, strong)NSArray *imageArray;@property(nonatomic, assign)CGFloat autoScrollTimeInterval;@property(nonatomic, weak)id<BannerViewDelegate> delegate;+ (instancetype)bannerViewWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray;@end</span>
.m文件实现
<span style="font-size:12px;">#import "BannerView.h"#import "BannerViewCell.h"#import <Masonry/Masonry.h>@interface BannerView () <UICollectionViewDataSource, UICollectionViewDelegate>@property(nonatomic, weak)UICollectionView *collectionView;@property(nonatomic, weak)UICollectionViewFlowLayout *flowLayout;@property(nonatomic, strong)NSTimer *timer;@property(nonatomic, assign)NSInteger totalItemsCount;@property(nonatomic, weak)UIPageControl *pageControl;@end@implementation BannerView+ (instancetype)bannerViewWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray{ BannerView *bannerView = [[self alloc]initWithFrame:frame]; bannerView.imageArray = imageArray; return bannerView;}- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor orangeColor]; [self SetupSubviews]; } return self;}- (void)SetupSubviews{ UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; flowLayout.itemSize = self.frame.size; flowLayout.minimumLineSpacing = 0; flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.flowLayout = flowLayout; UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.frame collectionViewLayout:flowLayout]; collectionView.pagingEnabled = YES; collectionView.showsHorizontalScrollIndicator = NO; collectionView.showsVerticalScrollIndicator = NO; [collectionView registerClass:[BannerViewCell class] forCellWithReuseIdentifier:ID]; collectionView.dataSource = self; collectionView.delegate = self; [self addSubview:collectionView]; self.collectionView = collectionView; UIPageControl *pageControl = [[UIPageControl alloc] init]; pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:85/255.0 green:132/255.0 blue:213/255.0 alpha:1.0]; pageControl.pageIndicatorTintColor = [UIColor whiteColor]; [self addSubview:pageControl]; self.pageControl = pageControl;}- (void)setFrame:(CGRect)frame{ [super setFrame:frame]; self.flowLayout.itemSize = self.frame.size;}- (void)setAutoScrollTimeInterval:(CGFloat)autoScrollTimeInterval{ _autoScrollTimeInterval = autoScrollTimeInterval; [_timer invalidate]; _timer = nil; [self setupTimer];}- (void)layoutSubviews{ [super layoutSubviews]; __weak __typeof(&*self)weakSelf = self; [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(weakSelf); make.bottom.mas_equalTo(weakSelf.mas_bottom); make.width.mas_equalTo(weakSelf.bounds.size.width/5.0); }]; _collectionView.frame = self.bounds; if (_collectionView.contentOffset.x == 0) { [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_totalItemsCount*0.5 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO]; }}- (void)setImageArray:(NSArray *)imageArray{ _imageArray = imageArray; self.pageControl.numberOfPages = imageArray.count; _totalItemsCount = imageArray.count*1000;}- (void)setupTimer{ NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:self.autoScrollTimeInterval target:self selector:@selector(automaticScroll) userInfo:nil repeats:YES]; _timer = timer; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];}- (void)automaticScroll{ int currentIndex = _collectionView.contentOffset.x / _flowLayout.itemSize.width; int targetIndex = currentIndex + 1; if (targetIndex == _totalItemsCount) { targetIndex = _totalItemsCount*0.5; [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO]; } [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];}#pragma mark -- UICollectionView DataSource- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return _totalItemsCount;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ BannerViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; long itemIndex = indexPath.item % self.imageArray.count; cell.imageView.image = self.imageArray[itemIndex]; return cell;}- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ long nIndex = indexPath.item%self.imageArray.count; if ([self.delegate respondsToSelector:@selector(bannerView:didSelectedAtIndex:)]) { [self.delegate bannerView:self didSelectedAtIndex:nIndex]; }}#pragma mark - UIScrollView Delegate- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ int currentIndex = _collectionView.contentOffset.x / _flowLayout.itemSize.width; NSInteger page = currentIndex%self.imageArray.count; self.pageControl.currentPage = page;}- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ [_timer invalidate]; _timer = nil;}- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ [self setupTimer];}@end</span>
自定义cell:
<span style="font-size:12px;">#import <UIKit/UIKit.h>@interface BannerViewCell : UICollectionViewCell@property (weak, nonatomic) UIImageView *imageView;@end</span>
<span style="font-size:12px;">#import "BannerViewCell.h"@implementation BannerViewCell- (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { UIImageView *imageView = [[UIImageView alloc] init]; [self.contentView addSubview:imageView]; _imageView = imageView; } return self;}- (void)layoutSubviews{ [super layoutSubviews]; _imageView.frame = self.contentView.frame;}@end</span>
0 0
- iOS开发之collectionView实现无限轮播视图
- collectionview实现无限轮播
- iOS -- 用collectionView实现图片的无限轮播
- iOS开发之无限轮播图片
- iOS 使用collectionView实现无线轮播
- collectionView 无限轮播设计思路
- iOS开发之ImageView复用实现图片无限轮播
- iOS开发之自定义视图上下无限循环滚动实现
- iOS之旅--scrollView实现无限轮播
- IOS开发之CollectionView
- ios 滚动视图,滚动广告,无限循环轮播
- 滚动视图 - 无限轮播
- IOS开发UI篇--使用UICollectionView实现一个无限轮播的案例
- iOS:实现图片的无限轮播---之使用第三方库SDCycleScrollView
- ios用collectionView实现无限滚动的简单日历
- iOS--集合视图CollectionView
- iOS 无限轮播图片
- iOS UICollectionView无限轮播
- 【剑指 offer】(十九)—— 二叉树镜像
- 关于C++中子类调用父类方法的一个问题
- 简单说说TCP(1) --- 建立连接三次握手
- Linux安装GCC
- 关于Java基础的一些笔试题总结
- iOS开发之collectionView实现无限轮播视图
- px、dp和sp,这些单位有什么区别
- Android获取当前连接的wifi名称
- Android RecyclerView添加分隔线 DividerItemDecoration
- C++ 虚函数表解析
- JSP中的<%=表达式%>和<%表达式%>有什么区别?
- PL/SQL练习题二(常用函数)
- sort用法
- Mac PlantUML