SDCycleScrollView.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. //
  2. // SDCycleScrollView.m
  3. // SDCycleScrollView
  4. //
  5. // Created by aier on 15-3-22.
  6. // Copyright (c) 2015年 GSD. All rights reserved.
  7. //
  8. /*
  9. *********************************************************************************
  10. *
  11. * 🌟🌟🌟 新建SDCycleScrollView交流QQ群:185534916 🌟🌟🌟
  12. *
  13. * 在您使用此自动轮播库的过程中如果出现bug请及时以以下任意一种方式联系我们,我们会及时修复bug并
  14. * 帮您解决问题。
  15. * 新浪微博:GSD_iOS
  16. * Email : gsdios@126.com
  17. * GitHub: https://github.com/gsdios
  18. *
  19. * 另(我的自动布局库SDAutoLayout):
  20. * 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于
  21. * 做最简单易用的AutoLayout库。
  22. * 视频教程:http://www.letv.com/ptv/vplay/24038772.html
  23. * 用法示例:https://github.com/gsdios/SDAutoLayout/blob/master/README.md
  24. * GitHub:https://github.com/gsdios/SDAutoLayout
  25. *********************************************************************************
  26. */
  27. #import "SDCycleScrollView.h"
  28. #import "UIView+SDExtension.h"
  29. #import "TAPageControl.h"
  30. #import "SDWebImageManager.h"
  31. #import "UIImageView+WebCache.h"
  32. #define kCycleScrollViewInitialPageControlDotSize CGSizeMake(10, 10)
  33. NSString * const ID = @"SDCycleScrollViewCell";
  34. @interface SDCycleScrollView () <UICollectionViewDataSource, UICollectionViewDelegate>
  35. @property (nonatomic, weak) UICollectionViewFlowLayout *flowLayout;
  36. @property (nonatomic, strong) NSArray *imagePathsGroup;
  37. @property (nonatomic, weak) NSTimer *timer;
  38. @property (nonatomic, assign) NSInteger totalItemsCount;
  39. @property (nonatomic, weak) UIControl *pageControl;
  40. @property (nonatomic, strong) UIImageView *backgroundImageView; // 当imageURLs为空时的背景图
  41. @end
  42. @implementation SDCycleScrollView
  43. - (instancetype)initWithFrame:(CGRect)frame
  44. {
  45. if (self = [super initWithFrame:frame]) {
  46. [self initialization];
  47. [self setupMainView];
  48. }
  49. return self;
  50. }
  51. - (void)awakeFromNib
  52. {
  53. [super awakeFromNib];
  54. [self initialization];
  55. [self setupMainView];
  56. }
  57. - (void)initialization
  58. {
  59. _pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
  60. _autoScrollTimeInterval = 2.0;
  61. _titleLabelTextColor = [UIColor whiteColor];
  62. _titleLabelTextFont= [UIFont systemFontOfSize:14];
  63. _titleLabelBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
  64. _titleLabelHeight = 30;
  65. _titleLabelTextAlignment = NSTextAlignmentLeft;
  66. _autoScroll = YES;
  67. _infiniteLoop = YES;
  68. _showPageControl = YES;
  69. _pageControlDotSize = kCycleScrollViewInitialPageControlDotSize;
  70. _pageControlBottomOffset = 0;
  71. _pageControlRightOffset = 0;
  72. _pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
  73. _hidesForSinglePage = YES;
  74. _currentPageDotColor = [UIColor whiteColor];
  75. _pageDotColor = [UIColor lightGrayColor];
  76. _bannerImageViewContentMode = UIViewContentModeScaleToFill;
  77. self.backgroundColor = [UIColor lightGrayColor];
  78. }
  79. + (instancetype)cycleScrollViewWithFrame:(CGRect)frame imageNamesGroup:(NSArray *)imageNamesGroup
  80. {
  81. SDCycleScrollView *cycleScrollView = [[self alloc] initWithFrame:frame];
  82. cycleScrollView.localizationImageNamesGroup = [NSMutableArray arrayWithArray:imageNamesGroup];
  83. return cycleScrollView;
  84. }
  85. + (instancetype)cycleScrollViewWithFrame:(CGRect)frame shouldInfiniteLoop:(BOOL)infiniteLoop imageNamesGroup:(NSArray *)imageNamesGroup
  86. {
  87. SDCycleScrollView *cycleScrollView = [[self alloc] initWithFrame:frame];
  88. cycleScrollView.infiniteLoop = infiniteLoop;
  89. cycleScrollView.localizationImageNamesGroup = [NSMutableArray arrayWithArray:imageNamesGroup];
  90. return cycleScrollView;
  91. }
  92. + (instancetype)cycleScrollViewWithFrame:(CGRect)frame imageURLStringsGroup:(NSArray *)imageURLsGroup
  93. {
  94. SDCycleScrollView *cycleScrollView = [[self alloc] initWithFrame:frame];
  95. cycleScrollView.imageURLStringsGroup = [NSMutableArray arrayWithArray:imageURLsGroup];
  96. return cycleScrollView;
  97. }
  98. + (instancetype)cycleScrollViewWithFrame:(CGRect)frame delegate:(id<SDCycleScrollViewDelegate>)delegate placeholderImage:(UIImage *)placeholderImage
  99. {
  100. SDCycleScrollView *cycleScrollView = [[self alloc] initWithFrame:frame];
  101. cycleScrollView.delegate = delegate;
  102. cycleScrollView.placeholderImage = placeholderImage;
  103. return cycleScrollView;
  104. }
  105. // 设置显示图片的collectionView
  106. - (void)setupMainView
  107. {
  108. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  109. flowLayout.minimumLineSpacing = 0;
  110. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  111. _flowLayout = flowLayout;
  112. UICollectionView *mainView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
  113. mainView.backgroundColor = [UIColor clearColor];
  114. mainView.pagingEnabled = YES;
  115. mainView.showsHorizontalScrollIndicator = NO;
  116. mainView.showsVerticalScrollIndicator = NO;
  117. [mainView registerClass:[SDCollectionViewCell class] forCellWithReuseIdentifier:ID];
  118. mainView.dataSource = self;
  119. mainView.delegate = self;
  120. mainView.scrollsToTop = NO;
  121. [self addSubview:mainView];
  122. _mainView = mainView;
  123. }
  124. #pragma mark - properties
  125. - (void)setDelegate:(id<SDCycleScrollViewDelegate>)delegate
  126. {
  127. _delegate = delegate;
  128. if ([self.delegate respondsToSelector:@selector(customCollectionViewCellClassForCycleScrollView:)] && [self.delegate customCollectionViewCellClassForCycleScrollView:self]) {
  129. [self.mainView registerClass:[self.delegate customCollectionViewCellClassForCycleScrollView:self] forCellWithReuseIdentifier:ID];
  130. }
  131. }
  132. - (void)setPlaceholderImage:(UIImage *)placeholderImage
  133. {
  134. _placeholderImage = placeholderImage;
  135. if (!self.backgroundImageView) {
  136. UIImageView *bgImageView = [UIImageView new];
  137. bgImageView.contentMode = UIViewContentModeScaleAspectFit;
  138. [self insertSubview:bgImageView belowSubview:self.mainView];
  139. self.backgroundImageView = bgImageView;
  140. }
  141. self.backgroundImageView.image = placeholderImage;
  142. }
  143. - (void)setPageControlDotSize:(CGSize)pageControlDotSize
  144. {
  145. _pageControlDotSize = pageControlDotSize;
  146. [self setupPageControl];
  147. if ([self.pageControl isKindOfClass:[TAPageControl class]]) {
  148. TAPageControl *pageContol = (TAPageControl *)_pageControl;
  149. pageContol.dotSize = pageControlDotSize;
  150. }
  151. }
  152. - (void)setShowPageControl:(BOOL)showPageControl
  153. {
  154. _showPageControl = showPageControl;
  155. _pageControl.hidden = !showPageControl;
  156. }
  157. - (void)setCurrentPageDotColor:(UIColor *)currentPageDotColor
  158. {
  159. _currentPageDotColor = currentPageDotColor;
  160. if ([self.pageControl isKindOfClass:[TAPageControl class]]) {
  161. TAPageControl *pageControl = (TAPageControl *)_pageControl;
  162. pageControl.dotColor = currentPageDotColor;
  163. } else {
  164. UIPageControl *pageControl = (UIPageControl *)_pageControl;
  165. pageControl.currentPageIndicatorTintColor = currentPageDotColor;
  166. }
  167. }
  168. - (void)setPageDotColor:(UIColor *)pageDotColor
  169. {
  170. _pageDotColor = pageDotColor;
  171. if ([self.pageControl isKindOfClass:[UIPageControl class]]) {
  172. UIPageControl *pageControl = (UIPageControl *)_pageControl;
  173. pageControl.pageIndicatorTintColor = pageDotColor;
  174. }
  175. }
  176. - (void)setCurrentPageDotImage:(UIImage *)currentPageDotImage
  177. {
  178. _currentPageDotImage = currentPageDotImage;
  179. if (self.pageControlStyle != SDCycleScrollViewPageContolStyleAnimated) {
  180. self.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
  181. }
  182. [self setCustomPageControlDotImage:currentPageDotImage isCurrentPageDot:YES];
  183. }
  184. - (void)setPageDotImage:(UIImage *)pageDotImage
  185. {
  186. _pageDotImage = pageDotImage;
  187. if (self.pageControlStyle != SDCycleScrollViewPageContolStyleAnimated) {
  188. self.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
  189. }
  190. [self setCustomPageControlDotImage:pageDotImage isCurrentPageDot:NO];
  191. }
  192. - (void)setCustomPageControlDotImage:(UIImage *)image isCurrentPageDot:(BOOL)isCurrentPageDot
  193. {
  194. if (!image || !self.pageControl) return;
  195. if ([self.pageControl isKindOfClass:[TAPageControl class]]) {
  196. TAPageControl *pageControl = (TAPageControl *)_pageControl;
  197. if (isCurrentPageDot) {
  198. pageControl.currentDotImage = image;
  199. } else {
  200. pageControl.dotImage = image;
  201. }
  202. }
  203. }
  204. - (void)setInfiniteLoop:(BOOL)infiniteLoop
  205. {
  206. _infiniteLoop = infiniteLoop;
  207. if (self.imagePathsGroup.count) {
  208. self.imagePathsGroup = self.imagePathsGroup;
  209. }
  210. }
  211. -(void)setAutoScroll:(BOOL)autoScroll{
  212. _autoScroll = autoScroll;
  213. [self invalidateTimer];
  214. if (_autoScroll) {
  215. [self setupTimer];
  216. }
  217. }
  218. - (void)setScrollDirection:(UICollectionViewScrollDirection)scrollDirection
  219. {
  220. _scrollDirection = scrollDirection;
  221. _flowLayout.scrollDirection = scrollDirection;
  222. }
  223. - (void)setAutoScrollTimeInterval:(CGFloat)autoScrollTimeInterval
  224. {
  225. _autoScrollTimeInterval = autoScrollTimeInterval;
  226. [self setAutoScroll:self.autoScroll];
  227. }
  228. - (void)setPageControlStyle:(SDCycleScrollViewPageContolStyle)pageControlStyle
  229. {
  230. _pageControlStyle = pageControlStyle;
  231. [self setupPageControl];
  232. }
  233. - (void)setImagePathsGroup:(NSArray *)imagePathsGroup
  234. {
  235. [self invalidateTimer];
  236. _imagePathsGroup = imagePathsGroup;
  237. _totalItemsCount = self.infiniteLoop ? self.imagePathsGroup.count * 100 : self.imagePathsGroup.count;
  238. if (imagePathsGroup.count > 1) { // 由于 !=1 包含count == 0等情况
  239. self.mainView.scrollEnabled = YES;
  240. [self setAutoScroll:self.autoScroll];
  241. } else {
  242. self.mainView.scrollEnabled = NO;
  243. [self setAutoScroll:NO];
  244. }
  245. [self setupPageControl];
  246. [self.mainView reloadData];
  247. }
  248. - (void)setImageURLStringsGroup:(NSArray *)imageURLStringsGroup
  249. {
  250. _imageURLStringsGroup = imageURLStringsGroup;
  251. NSMutableArray *temp = [NSMutableArray new];
  252. [_imageURLStringsGroup enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * stop) {
  253. NSString *urlString;
  254. if ([obj isKindOfClass:[NSString class]]) {
  255. urlString = obj;
  256. } else if ([obj isKindOfClass:[NSURL class]]) {
  257. NSURL *url = (NSURL *)obj;
  258. urlString = [url absoluteString];
  259. }
  260. if (urlString) {
  261. [temp addObject:urlString];
  262. }
  263. }];
  264. self.imagePathsGroup = [temp copy];
  265. }
  266. - (void)setLocalizationImageNamesGroup:(NSArray *)localizationImageNamesGroup
  267. {
  268. _localizationImageNamesGroup = localizationImageNamesGroup;
  269. self.imagePathsGroup = [localizationImageNamesGroup copy];
  270. }
  271. - (void)setTitlesGroup:(NSArray *)titlesGroup
  272. {
  273. _titlesGroup = titlesGroup;
  274. if (self.onlyDisplayText) {
  275. NSMutableArray *temp = [NSMutableArray new];
  276. for (int i = 0; i < _titlesGroup.count; i++) {
  277. [temp addObject:@""];
  278. }
  279. self.backgroundColor = [UIColor clearColor];
  280. self.imageURLStringsGroup = [temp copy];
  281. }
  282. }
  283. - (void)disableScrollGesture {
  284. self.mainView.canCancelContentTouches = NO;
  285. for (UIGestureRecognizer *gesture in self.mainView.gestureRecognizers) {
  286. if ([gesture isKindOfClass:[UIPanGestureRecognizer class]]) {
  287. [self.mainView removeGestureRecognizer:gesture];
  288. }
  289. }
  290. }
  291. #pragma mark - actions
  292. - (void)setupTimer
  293. {
  294. [self invalidateTimer]; // 创建定时器前先停止定时器,不然会出现僵尸定时器,导致轮播频率错误
  295. NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:self.autoScrollTimeInterval target:self selector:@selector(automaticScroll) userInfo:nil repeats:YES];
  296. _timer = timer;
  297. [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
  298. }
  299. - (void)invalidateTimer
  300. {
  301. [_timer invalidate];
  302. _timer = nil;
  303. }
  304. - (void)setupPageControl
  305. {
  306. if (_pageControl) [_pageControl removeFromSuperview]; // 重新加载数据时调整
  307. if (self.imagePathsGroup.count == 0 || self.onlyDisplayText) return;
  308. if ((self.imagePathsGroup.count == 1) && self.hidesForSinglePage) return;
  309. int indexOnPageControl = [self pageControlIndexWithCurrentCellIndex:[self currentIndex]];
  310. switch (self.pageControlStyle) {
  311. case SDCycleScrollViewPageContolStyleAnimated:
  312. {
  313. TAPageControl *pageControl = [[TAPageControl alloc] init];
  314. pageControl.numberOfPages = self.imagePathsGroup.count;
  315. pageControl.dotColor = self.currentPageDotColor;
  316. pageControl.userInteractionEnabled = NO;
  317. pageControl.currentPage = indexOnPageControl;
  318. [self addSubview:pageControl];
  319. _pageControl = pageControl;
  320. }
  321. break;
  322. case SDCycleScrollViewPageContolStyleClassic:
  323. {
  324. UIPageControl *pageControl = [[UIPageControl alloc] init];
  325. pageControl.numberOfPages = self.imagePathsGroup.count;
  326. pageControl.currentPageIndicatorTintColor = self.currentPageDotColor;
  327. pageControl.pageIndicatorTintColor = self.pageDotColor;
  328. pageControl.userInteractionEnabled = NO;
  329. pageControl.currentPage = indexOnPageControl;
  330. [self addSubview:pageControl];
  331. _pageControl = pageControl;
  332. }
  333. break;
  334. default:
  335. break;
  336. }
  337. // 重设pagecontroldot图片
  338. if (self.currentPageDotImage) {
  339. self.currentPageDotImage = self.currentPageDotImage;
  340. }
  341. if (self.pageDotImage) {
  342. self.pageDotImage = self.pageDotImage;
  343. }
  344. }
  345. - (void)automaticScroll
  346. {
  347. if (0 == _totalItemsCount) return;
  348. int currentIndex = [self currentIndex];
  349. int targetIndex = currentIndex + 1;
  350. [self scrollToIndex:targetIndex];
  351. }
  352. - (void)scrollToIndex:(int)targetIndex
  353. {
  354. if (targetIndex >= _totalItemsCount) {
  355. if (self.infiniteLoop) {
  356. targetIndex = _totalItemsCount * 0.5;
  357. [_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  358. }
  359. return;
  360. }
  361. [_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
  362. }
  363. - (int)currentIndex
  364. {
  365. if (_mainView.sd_width == 0 || _mainView.sd_height == 0) {
  366. return 0;
  367. }
  368. int index = 0;
  369. if (_flowLayout.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
  370. index = (_mainView.contentOffset.x + _flowLayout.itemSize.width * 0.5) / _flowLayout.itemSize.width;
  371. } else {
  372. index = (_mainView.contentOffset.y + _flowLayout.itemSize.height * 0.5) / _flowLayout.itemSize.height;
  373. }
  374. return MAX(0, index);
  375. }
  376. - (void)makeScrollViewScrollToIndex:(NSInteger)index{
  377. if (self.autoScroll) {
  378. [self invalidateTimer];
  379. }
  380. if (0 == _totalItemsCount) return;
  381. [self scrollToIndex:(int)(_totalItemsCount * 0.5 + index)];
  382. if (self.autoScroll) {
  383. [self setupTimer];
  384. }
  385. }
  386. - (int)pageControlIndexWithCurrentCellIndex:(NSInteger)index
  387. {
  388. return (int)index % self.imagePathsGroup.count;
  389. }
  390. - (void)clearCache
  391. {
  392. [[self class] clearImagesCache];
  393. }
  394. + (void)clearImagesCache
  395. {
  396. [[[SDWebImageManager sharedManager] imageCache] clearDiskOnCompletion:nil];
  397. }
  398. #pragma mark - life circles
  399. - (void)layoutSubviews
  400. {
  401. self.delegate = self.delegate;
  402. [super layoutSubviews];
  403. _flowLayout.itemSize = self.frame.size;
  404. _mainView.frame = self.bounds;
  405. if (_mainView.contentOffset.x == 0 && _totalItemsCount) {
  406. int targetIndex = 0;
  407. if (self.infiniteLoop) {
  408. targetIndex = _totalItemsCount * 0.5;
  409. }else{
  410. targetIndex = 0;
  411. }
  412. [_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  413. }
  414. CGSize size = CGSizeZero;
  415. if ([self.pageControl isKindOfClass:[TAPageControl class]]) {
  416. TAPageControl *pageControl = (TAPageControl *)_pageControl;
  417. if (!(self.pageDotImage && self.currentPageDotImage && CGSizeEqualToSize(kCycleScrollViewInitialPageControlDotSize, self.pageControlDotSize))) {
  418. pageControl.dotSize = self.pageControlDotSize;
  419. }
  420. size = [pageControl sizeForNumberOfPages:self.imagePathsGroup.count];
  421. } else {
  422. size = CGSizeMake(self.imagePathsGroup.count * self.pageControlDotSize.width * 1.5, self.pageControlDotSize.height);
  423. }
  424. CGFloat x = (self.sd_width - size.width) * 0.5;
  425. if (self.pageControlAliment == SDCycleScrollViewPageContolAlimentRight) {
  426. x = self.mainView.sd_width - size.width - 10;
  427. }
  428. CGFloat y = self.mainView.sd_height - size.height - 10;
  429. if ([self.pageControl isKindOfClass:[TAPageControl class]]) {
  430. TAPageControl *pageControl = (TAPageControl *)_pageControl;
  431. [pageControl sizeToFit];
  432. }
  433. CGRect pageControlFrame = CGRectMake(x, y, size.width, size.height);
  434. pageControlFrame.origin.y -= self.pageControlBottomOffset;
  435. pageControlFrame.origin.x -= self.pageControlRightOffset;
  436. self.pageControl.frame = pageControlFrame;
  437. self.pageControl.hidden = !_showPageControl;
  438. if (self.backgroundImageView) {
  439. self.backgroundImageView.frame = self.bounds;
  440. }
  441. }
  442. //解决当父View释放时,当前视图因为被Timer强引用而不能释放的问题
  443. - (void)willMoveToSuperview:(UIView *)newSuperview
  444. {
  445. if (!newSuperview) {
  446. [self invalidateTimer];
  447. }
  448. }
  449. //解决当timer释放后 回调scrollViewDidScroll时访问野指针导致崩溃
  450. - (void)dealloc {
  451. _mainView.delegate = nil;
  452. _mainView.dataSource = nil;
  453. }
  454. #pragma mark - public actions
  455. - (void)adjustWhenControllerViewWillAppera
  456. {
  457. long targetIndex = [self currentIndex];
  458. if (targetIndex < _totalItemsCount) {
  459. [_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  460. }
  461. }
  462. #pragma mark - UICollectionViewDataSource
  463. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  464. {
  465. return _totalItemsCount;
  466. }
  467. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  468. {
  469. SDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
  470. long itemIndex = [self pageControlIndexWithCurrentCellIndex:indexPath.item];
  471. if ([self.delegate respondsToSelector:@selector(setupCustomCell:forIndex:cycleScrollView:)] &&
  472. [self.delegate respondsToSelector:@selector(customCollectionViewCellClassForCycleScrollView:)] &&
  473. [self.delegate customCollectionViewCellClassForCycleScrollView:self]) {
  474. [self.delegate setupCustomCell:cell forIndex:itemIndex cycleScrollView:self];
  475. return cell;
  476. }
  477. NSString *imagePath = self.imagePathsGroup[itemIndex];
  478. if (!self.onlyDisplayText && [imagePath isKindOfClass:[NSString class]]) {
  479. if ([imagePath hasPrefix:@"http"]) {
  480. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:self.placeholderImage];
  481. } else {
  482. UIImage *image = [UIImage imageNamed:imagePath];
  483. if (!image) {
  484. [UIImage imageWithContentsOfFile:imagePath];
  485. }
  486. cell.imageView.image = image;
  487. }
  488. } else if (!self.onlyDisplayText && [imagePath isKindOfClass:[UIImage class]]) {
  489. cell.imageView.image = (UIImage *)imagePath;
  490. }
  491. if (_titlesGroup.count && itemIndex < _titlesGroup.count) {
  492. cell.title = _titlesGroup[itemIndex];
  493. }
  494. if (!cell.hasConfigured) {
  495. cell.titleLabelBackgroundColor = self.titleLabelBackgroundColor;
  496. cell.titleLabelHeight = self.titleLabelHeight;
  497. cell.titleLabelTextAlignment = self.titleLabelTextAlignment;
  498. cell.titleLabelTextColor = self.titleLabelTextColor;
  499. cell.titleLabelTextFont = self.titleLabelTextFont;
  500. cell.hasConfigured = YES;
  501. cell.imageView.contentMode = self.bannerImageViewContentMode;
  502. cell.clipsToBounds = YES;
  503. cell.onlyDisplayText = self.onlyDisplayText;
  504. }
  505. return cell;
  506. }
  507. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  508. {
  509. if ([self.delegate respondsToSelector:@selector(cycleScrollView:didSelectItemAtIndex:)]) {
  510. [self.delegate cycleScrollView:self didSelectItemAtIndex:[self pageControlIndexWithCurrentCellIndex:indexPath.item]];
  511. }
  512. if (self.clickItemOperationBlock) {
  513. self.clickItemOperationBlock([self pageControlIndexWithCurrentCellIndex:indexPath.item]);
  514. }
  515. }
  516. #pragma mark - UIScrollViewDelegate
  517. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  518. {
  519. if (!self.imagePathsGroup.count) return; // 解决清除timer时偶尔会出现的问题
  520. int itemIndex = [self currentIndex];
  521. int indexOnPageControl = [self pageControlIndexWithCurrentCellIndex:itemIndex];
  522. if ([self.pageControl isKindOfClass:[TAPageControl class]]) {
  523. TAPageControl *pageControl = (TAPageControl *)_pageControl;
  524. pageControl.currentPage = indexOnPageControl;
  525. } else {
  526. UIPageControl *pageControl = (UIPageControl *)_pageControl;
  527. pageControl.currentPage = indexOnPageControl;
  528. }
  529. }
  530. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  531. {
  532. if (self.autoScroll) {
  533. [self invalidateTimer];
  534. }
  535. }
  536. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  537. {
  538. if (self.autoScroll) {
  539. [self setupTimer];
  540. }
  541. }
  542. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  543. {
  544. [self scrollViewDidEndScrollingAnimation:self.mainView];
  545. }
  546. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
  547. {
  548. if (!self.imagePathsGroup.count) return; // 解决清除timer时偶尔会出现的问题
  549. int itemIndex = [self currentIndex];
  550. int indexOnPageControl = [self pageControlIndexWithCurrentCellIndex:itemIndex];
  551. if ([self.delegate respondsToSelector:@selector(cycleScrollView:didScrollToIndex:)]) {
  552. [self.delegate cycleScrollView:self didScrollToIndex:indexOnPageControl];
  553. } else if (self.itemDidScrollOperationBlock) {
  554. self.itemDidScrollOperationBlock(indexOnPageControl);
  555. }
  556. }
  557. @end