| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- //
- // SDCycleScrollView.m
- // SDCycleScrollView
- //
- // Created by aier on 15-3-22.
- // Copyright (c) 2015年 GSD. All rights reserved.
- //
- /**
-
- *******************************************************
- * *
- * 感谢您的支持, 如果下载的代码在使用过程中出现BUG或者其他问题 *
- * 您可以发邮件到gsdios@126.com 或者 到 *
- * https://github.com/gsdios?tab=repositories 提交问题 *
- * *
- *******************************************************
-
- */
- #import "SDCycleScrollView2.h"
- #import "SDCollectionViewCell.h"
- #import "UIView+SDExtension.h"
- #import "TAPageControl.h"
- //#import "NSData+SDDataCache.h"
- #define kRepeatCount 1 // 循环次数
- NSString * const MYID = @"cycleCell2";
- @interface SDCycleScrollView2 () <UICollectionViewDataSource, UICollectionViewDelegate>
- @property (nonatomic, weak) UICollectionView *mainView; // 显示图片的collectionView
- @property (nonatomic, weak) UICollectionViewFlowLayout *flowLayout;
- @property (nonatomic, strong) NSMutableArray *imagesGroup;
- @property (nonatomic, strong) NSTimer *timer;
- @property (nonatomic, assign) NSInteger totalItemsCount;
- @property (nonatomic, weak) TAPageControl *pageControl;
- @end
- @implementation SDCycleScrollView2
- - (instancetype)initWithFrame:(CGRect)frame
- {
- if (self = [super initWithFrame:frame]) {
- [self initialization];
- [self setupMainView];
- }
- return self;
- }
- - (id)initWithCoder:(NSCoder *)aDecoder
- {
- if (self = [super initWithCoder:aDecoder]) {
- [self initialization];
- [self setupMainView];
- }
- return self;
- }
- - (void)initialization
- {
- _pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
- _autoScrollTimeInterval = 1.0;
- _titleLabelTextColor = [UIColor whiteColor];
- _titleLabelTextFont= [UIFont systemFontOfSize:14];
- _titleLabelBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
- _titleLabelHeight = 30;
- self.backgroundColor = [UIColor clearColor];
- }
- + (instancetype)cycleScrollViewWithFrame:(CGRect)frame imagesGroup:(NSArray *)imagesGroup
- {
- SDCycleScrollView2 *cycleScrollView = [[self alloc] initWithFrame:frame];
- cycleScrollView.imagesGroup = [NSMutableArray arrayWithArray:imagesGroup];
- return cycleScrollView;
- }
- + (instancetype)cycleScrollViewWithFrame:(CGRect)frame imageURLsGroup:(NSArray *)imageURLsGroup
- {
- SDCycleScrollView2 *cycleScrollView = [[self alloc] initWithFrame:frame];
- cycleScrollView.imageURLsGroup = [NSMutableArray arrayWithArray:imageURLsGroup];
- return cycleScrollView;
- }
- - (void)setFrame:(CGRect)frame
- {
- [super setFrame:frame];
-
- _flowLayout.itemSize = self.frame.size;
- }
- - (void)setPageControlDotSize:(CGSize)pageControlDotSize
- {
- _pageControlDotSize = pageControlDotSize;
- _pageControl.dotSize = pageControlDotSize;
- }
- - (void)setDotColor:(UIColor *)dotColor
- {
- _dotColor = dotColor;
- _pageControl.dotColor = dotColor;
- }
- - (void)setAutoScrollTimeInterval:(CGFloat)autoScrollTimeInterval
- {
- _autoScrollTimeInterval = autoScrollTimeInterval;
-
- [_timer invalidate];
- _timer = nil;
- }
- // 设置显示图片的collectionView
- - (void)setupMainView
- {
- UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
- flowLayout.itemSize = self.frame.size;
- flowLayout.minimumLineSpacing = 0;
- flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- _flowLayout = flowLayout;
-
- UICollectionView *mainView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:flowLayout];
- mainView.backgroundColor = [UIColor clearColor];
- mainView.pagingEnabled = YES;
- mainView.showsHorizontalScrollIndicator = NO;
- mainView.showsVerticalScrollIndicator = NO;
- mainView.bounces = NO;
- [mainView registerClass:[SDCollectionViewCell class] forCellWithReuseIdentifier:MYID];
- mainView.dataSource = self;
- mainView.delegate = self;
- [self addSubview:mainView];
- _mainView = mainView;
- }
- - (void)setImagesGroup:(NSMutableArray *)imagesGroup
- {
- if ([imagesGroup count]>0) {
- _imagesGroup = imagesGroup;
- _totalItemsCount = imagesGroup.count * kRepeatCount;
-
- [self setupPageControl];
- }
-
- }
- - (void)setupPageControl
- {
- if ([_imagesGroup count]>1)
- {
- if (_pageControl) [_pageControl removeFromSuperview];//重新加载数据时调整
- TAPageControl *pageControl = [[TAPageControl alloc] init];
- pageControl.numberOfPages = self.imagesGroup.count;
- [self addSubview:pageControl];
- _pageControl = pageControl;
- }
- }
- - (void)layoutSubviews
- {
- [super layoutSubviews];
-
- if ([_imagesGroup count]>1) {
- _mainView.frame = self.bounds;
- // if (_mainView.contentOffset.x == 0 && _totalItemsCount) {
- // [_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_totalItemsCount * 0.5 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
- // }
-
- CGSize size = [_pageControl sizeForNumberOfPages:self.imagesGroup.count];
- CGFloat x = (self.sd_width - size.width) * 0.5;
- if (self.pageControlAliment == SDCycleScrollViewPageContolAlimentRight) {
- x = self.mainView.sd_width - size.width - 10;
- }
- CGFloat y = self.mainView.sd_height - size.height - 10;
- _pageControl.frame = CGRectMake(x, y, size.width, size.height);
- [_pageControl sizeToFit];
- }
-
- }
- //解决当父View释放时,当前视图因为被Timer强引用而不能释放的问题
- - (void)willMoveToSuperview:(UIView *)newSuperview
- {
- if (!newSuperview) {
- [_timer invalidate];
- _timer = nil;
- }
- }
- #pragma mark - UICollectionViewDataSource
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
- return _totalItemsCount;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- SDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MYID forIndexPath:indexPath];
- for (UIView *view in cell.subviews) {
- if (view) {
- [view removeFromSuperview];
- }
- }
-
- long itemIndex = indexPath.item % self.imagesGroup.count;
- [cell addSubview:self.imagesGroup[itemIndex]];
-
- if (!cell.hasConfigured) {
- cell.titleLabelBackgroundColor = self.titleLabelBackgroundColor;
- cell.titleLabelHeight = self.titleLabelHeight;
- cell.titleLabelTextColor = self.titleLabelTextColor;
- cell.titleLabelTextFont = self.titleLabelTextFont;
- cell.hasConfigured = YES;
- }
-
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- if ([self.delegate respondsToSelector:@selector(cycleScrollView:didSelectItemAtIndex:)]) {
- [self.delegate cycleScrollView:self didSelectItemAtIndex:indexPath.item % self.imagesGroup.count];
- }
- }
- #pragma mark - UIScrollViewDelegate
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView
- {
- if ([_imagesGroup count]>1) {
- int itemIndex = (scrollView.contentOffset.x + self.mainView.sd_width * 0.5) / self.mainView.sd_width;
- if (!self.imagesGroup.count) return; // 解决清除timer时偶尔会出现的问题
- int indexOnPageControl = itemIndex % self.imagesGroup.count;
- _pageControl.currentPage = indexOnPageControl;
- }
-
- }
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
- {
- [_timer invalidate];
- _timer = nil;
- }
- @end
|