Keine Beschreibung

SDCycleScrollView.m 23KB

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