口袋优选

SDCycleScrollView.m 21KB

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