TYCyclePagerView.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. //
  2. // TYCyclePagerView.m
  3. // TYCyclePagerViewDemo
  4. //
  5. // Created by tany on 2017/6/14.
  6. // Copyright © 2017年 tany. All rights reserved.
  7. //
  8. #import "TYCyclePagerView.h"
  9. NS_INLINE BOOL TYEqualIndexSection(TYIndexSection indexSection1,TYIndexSection indexSection2) {
  10. return indexSection1.index == indexSection2.index && indexSection1.section == indexSection2.section;
  11. }
  12. NS_INLINE TYIndexSection TYMakeIndexSection(NSInteger index, NSInteger section) {
  13. TYIndexSection indexSection;
  14. indexSection.index = index;
  15. indexSection.section = section;
  16. return indexSection;
  17. }
  18. @interface TYCyclePagerView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, TYCyclePagerTransformLayoutDelegate> {
  19. struct {
  20. unsigned int pagerViewDidScroll :1;
  21. unsigned int didScrollFromIndexToNewIndex :1;
  22. unsigned int initializeTransformAttributes :1;
  23. unsigned int applyTransformToAttributes :1;
  24. }_delegateFlags;
  25. struct {
  26. unsigned int cellForItemAtIndex :1;
  27. unsigned int layoutForPagerView :1;
  28. }_dataSourceFlags;
  29. }
  30. // UI
  31. @property (nonatomic, weak) UICollectionView *collectionView;
  32. @property (nonatomic, strong) NSTimer *timer;
  33. // Data
  34. @property (nonatomic, assign) NSInteger numberOfItems;
  35. @property (nonatomic, assign) NSInteger dequeueSection;
  36. @property (nonatomic, assign) TYIndexSection beginDragIndexSection;
  37. @property (nonatomic, assign) NSInteger firstScrollIndex;
  38. @property (nonatomic, assign) BOOL needClearLayout;
  39. @property (nonatomic, assign) BOOL didReloadData;
  40. @property (nonatomic, assign) BOOL didLayout;
  41. @property (nonatomic, assign) BOOL needResetIndex;
  42. @end
  43. #define kPagerViewMaxSectionCount 200
  44. #define kPagerViewMinSectionCount 18
  45. @implementation TYCyclePagerView
  46. #pragma mark - life Cycle
  47. - (instancetype)initWithFrame:(CGRect)frame {
  48. if (self = [super initWithFrame:frame]) {
  49. [self configureProperty];
  50. [self addCollectionView];
  51. }
  52. return self;
  53. }
  54. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  55. if (self = [super initWithCoder:aDecoder]) {
  56. [self configureProperty];
  57. [self addCollectionView];
  58. }
  59. return self;
  60. }
  61. - (void)configureProperty {
  62. _needResetIndex = NO;
  63. _didReloadData = NO;
  64. _didLayout = NO;
  65. _autoScrollInterval = 0;
  66. _isInfiniteLoop = YES;
  67. _beginDragIndexSection.index = 0;
  68. _beginDragIndexSection.section = 0;
  69. _indexSection.index = -1;
  70. _indexSection.section = -1;
  71. _firstScrollIndex = -1;
  72. }
  73. - (void)addCollectionView {
  74. TYCyclePagerTransformLayout *layout = [[TYCyclePagerTransformLayout alloc]init];
  75. UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
  76. layout.delegate = _delegateFlags.applyTransformToAttributes ? self : nil;;
  77. collectionView.backgroundColor = [UIColor clearColor];
  78. collectionView.dataSource = self;
  79. collectionView.delegate = self;
  80. collectionView.pagingEnabled = NO;
  81. collectionView.decelerationRate = 1-0.0076;
  82. if ([collectionView respondsToSelector:@selector(setPrefetchingEnabled:)]) {
  83. collectionView.prefetchingEnabled = NO;
  84. }
  85. collectionView.showsHorizontalScrollIndicator = NO;
  86. collectionView.showsVerticalScrollIndicator = NO;
  87. [self addSubview:collectionView];
  88. _collectionView = collectionView;
  89. }
  90. - (void)willMoveToSuperview:(UIView *)newSuperview {
  91. if (!newSuperview) {
  92. [self removeTimer];
  93. }else {
  94. [self removeTimer];
  95. if (_autoScrollInterval > 0) {
  96. [self addTimer];
  97. }
  98. }
  99. }
  100. #pragma mark - timer
  101. - (void)addTimer {
  102. if (_timer || _autoScrollInterval <= 0) {
  103. return;
  104. }
  105. _timer = [NSTimer timerWithTimeInterval:_autoScrollInterval target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
  106. [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  107. }
  108. - (void)removeTimer {
  109. if (!_timer) {
  110. return;
  111. }
  112. [_timer invalidate];
  113. _timer = nil;
  114. }
  115. - (void)timerFired:(NSTimer *)timer {
  116. if (!self.superview || !self.window || _numberOfItems == 0 || self.tracking) {
  117. return;
  118. }
  119. [self scrollToNearlyIndexAtDirection:TYPagerScrollDirectionRight animate:YES];
  120. }
  121. #pragma mark - getter
  122. - (TYCyclePagerViewLayout *)layout {
  123. if (!_layout) {
  124. if (_dataSourceFlags.layoutForPagerView) {
  125. _layout = [_dataSource layoutForPagerView:self];
  126. _layout.isInfiniteLoop = _isInfiniteLoop;
  127. }
  128. if (_layout.itemSize.width <= 0 || _layout.itemSize.height <= 0) {
  129. _layout = nil;
  130. }
  131. }
  132. return _layout;
  133. }
  134. - (NSInteger)curIndex {
  135. return _indexSection.index;
  136. }
  137. - (CGPoint)contentOffset {
  138. return _collectionView.contentOffset;
  139. }
  140. - (BOOL)tracking {
  141. return _collectionView.tracking;
  142. }
  143. - (BOOL)dragging {
  144. return _collectionView.dragging;
  145. }
  146. - (BOOL)decelerating {
  147. return _collectionView.decelerating;
  148. }
  149. - (UIView *)backgroundView {
  150. return _collectionView.backgroundView;
  151. }
  152. - (__kindof UICollectionViewCell *)curIndexCell {
  153. return [_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_indexSection.index inSection:_indexSection.section]];
  154. }
  155. - (NSArray<__kindof UICollectionViewCell *> *)visibleCells {
  156. return _collectionView.visibleCells;
  157. }
  158. - (NSArray *)visibleIndexs {
  159. NSMutableArray *indexs = [NSMutableArray array];
  160. for (NSIndexPath *indexPath in _collectionView.indexPathsForVisibleItems) {
  161. [indexs addObject:@(indexPath.item)];
  162. }
  163. return [indexs copy];
  164. }
  165. #pragma mark - setter
  166. - (void)setBackgroundView:(UIView *)backgroundView {
  167. [_collectionView setBackgroundView:backgroundView];
  168. }
  169. - (void)setAutoScrollInterval:(CGFloat)autoScrollInterval {
  170. _autoScrollInterval = autoScrollInterval;
  171. [self removeTimer];
  172. if (autoScrollInterval > 0 && self.superview) {
  173. [self addTimer];
  174. }
  175. }
  176. - (void)setDelegate:(id<TYCyclePagerViewDelegate>)delegate {
  177. _delegate = delegate;
  178. _delegateFlags.pagerViewDidScroll = [delegate respondsToSelector:@selector(pagerViewDidScroll:)];
  179. _delegateFlags.didScrollFromIndexToNewIndex = [delegate respondsToSelector:@selector(pagerView:didScrollFromIndex:toIndex:)];
  180. _delegateFlags.initializeTransformAttributes = [delegate respondsToSelector:@selector(pagerView:initializeTransformAttributes:)];
  181. _delegateFlags.applyTransformToAttributes = [delegate respondsToSelector:@selector(pagerView:applyTransformToAttributes:)];
  182. if (self.collectionView && self.collectionView.collectionViewLayout) {
  183. ((TYCyclePagerTransformLayout *)self.collectionView.collectionViewLayout).delegate = _delegateFlags.applyTransformToAttributes ? self : nil;
  184. }
  185. }
  186. - (void)setDataSource:(id<TYCyclePagerViewDataSource>)dataSource {
  187. _dataSource = dataSource;
  188. _dataSourceFlags.cellForItemAtIndex = [dataSource respondsToSelector:@selector(pagerView:cellForItemAtIndex:)];
  189. _dataSourceFlags.layoutForPagerView = [dataSource respondsToSelector:@selector(layoutForPagerView:)];
  190. }
  191. #pragma mark - public
  192. - (void)reloadData {
  193. _didReloadData = YES;
  194. _needResetIndex = YES;
  195. [self setNeedClearLayout];
  196. [self clearLayout];
  197. [self updateData];
  198. }
  199. // not clear layout
  200. - (void)updateData {
  201. [self updateLayout];
  202. _numberOfItems = [_dataSource numberOfItemsInPagerView:self];
  203. [_collectionView reloadData];
  204. if (!_didLayout && !CGRectIsEmpty(self.collectionView.frame) && _indexSection.index < 0) {
  205. _didLayout = YES;
  206. }
  207. BOOL needResetIndex = _needResetIndex && _reloadDataNeedResetIndex;
  208. _needResetIndex = NO;
  209. if (needResetIndex) {
  210. [self removeTimer];
  211. }
  212. [self resetPagerViewAtIndex:(_indexSection.index < 0 && !CGRectIsEmpty(self.collectionView.frame)) || needResetIndex ? 0 :_indexSection.index];
  213. if (needResetIndex) {
  214. [self addTimer];
  215. }
  216. }
  217. - (void)scrollToNearlyIndexAtDirection:(TYPagerScrollDirection)direction animate:(BOOL)animate {
  218. TYIndexSection indexSection = [self nearlyIndexPathAtDirection:direction];
  219. [self scrollToItemAtIndexSection:indexSection animate:animate];
  220. }
  221. - (void)scrollToItemAtIndex:(NSInteger)index animate:(BOOL)animate {
  222. if (!_didLayout && _didReloadData) {
  223. _firstScrollIndex = index;
  224. }else {
  225. _firstScrollIndex = -1;
  226. }
  227. if (!_isInfiniteLoop) {
  228. [self scrollToItemAtIndexSection:TYMakeIndexSection(index, 0) animate:animate];
  229. return;
  230. }
  231. [self scrollToItemAtIndexSection:TYMakeIndexSection(index, index >= self.curIndex ? _indexSection.section : _indexSection.section+1) animate:animate];
  232. }
  233. - (void)scrollToItemAtIndexSection:(TYIndexSection)indexSection animate:(BOOL)animate {
  234. if (_numberOfItems <= 0 || ![self isValidIndexSection:indexSection]) {
  235. //MOLogV(@"scrollToItemAtIndex: item indexSection is invalid!");
  236. return;
  237. }
  238. if (animate && [_delegate respondsToSelector:@selector(pagerViewWillBeginScrollingAnimation:)]) {
  239. [_delegate pagerViewWillBeginScrollingAnimation:self];
  240. }
  241. CGFloat offset = [self caculateOffsetXAtIndexSection:indexSection];
  242. if(_layout.scrollDirection == TYCyclePagerScrollDirectionHorizontal){
  243. [_collectionView setContentOffset:CGPointMake(offset, _collectionView.contentOffset.y) animated:animate];
  244. }
  245. else{
  246. [_collectionView setContentOffset:CGPointMake(_collectionView.contentOffset.x, offset) animated:animate];
  247. }
  248. }
  249. - (void)registerClass:(Class)Class forCellWithReuseIdentifier:(NSString *)identifier {
  250. [_collectionView registerClass:Class forCellWithReuseIdentifier:identifier];
  251. }
  252. - (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier {
  253. [_collectionView registerNib:nib forCellWithReuseIdentifier:identifier];
  254. }
  255. - (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndex:(NSInteger)index {
  256. UICollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:[NSIndexPath indexPathForItem:index inSection:_dequeueSection]];
  257. return cell;
  258. }
  259. #pragma mark - configure layout
  260. - (void)updateLayout {
  261. if (!self.layout) {
  262. return;
  263. }
  264. self.layout.isInfiniteLoop = _isInfiniteLoop;
  265. ((TYCyclePagerTransformLayout *)_collectionView.collectionViewLayout).layout = self.layout;
  266. }
  267. - (void)clearLayout {
  268. if (_needClearLayout) {
  269. _layout = nil;
  270. _needClearLayout = NO;
  271. }
  272. }
  273. - (void)setNeedClearLayout {
  274. _needClearLayout = YES;
  275. }
  276. - (void)setNeedUpdateLayout {
  277. if (!self.layout) {
  278. return;
  279. }
  280. [self clearLayout];
  281. [self updateLayout];
  282. [_collectionView.collectionViewLayout invalidateLayout];
  283. [self resetPagerViewAtIndex:_indexSection.index < 0 ? 0 :_indexSection.index];
  284. }
  285. #pragma mark - pager index
  286. - (BOOL)isValidIndexSection:(TYIndexSection)indexSection {
  287. return indexSection.index >= 0 && indexSection.index < _numberOfItems && indexSection.section >= 0 && indexSection.section < kPagerViewMaxSectionCount;
  288. }
  289. - (TYIndexSection)nearlyIndexPathAtDirection:(TYPagerScrollDirection)direction{
  290. return [self nearlyIndexPathForIndexSection:_indexSection direction:direction];
  291. }
  292. - (TYIndexSection)nearlyIndexPathForIndexSection:(TYIndexSection)indexSection direction:(TYPagerScrollDirection)direction {
  293. if (indexSection.index < 0 || indexSection.index >= _numberOfItems) {
  294. return indexSection;
  295. }
  296. if (!_isInfiniteLoop) {
  297. if (direction == TYPagerScrollDirectionRight && indexSection.index == _numberOfItems - 1) {
  298. return _autoScrollInterval > 0 ? TYMakeIndexSection(0, 0) : indexSection;
  299. } else if (direction == TYPagerScrollDirectionRight) {
  300. return TYMakeIndexSection(indexSection.index+1, 0);
  301. }
  302. if (indexSection.index == 0) {
  303. return _autoScrollInterval > 0 ? TYMakeIndexSection(_numberOfItems - 1, 0) : indexSection;
  304. }
  305. return TYMakeIndexSection(indexSection.index-1, 0);
  306. }
  307. if (direction == TYPagerScrollDirectionRight) {
  308. if (indexSection.index < _numberOfItems-1) {
  309. return TYMakeIndexSection(indexSection.index+1, indexSection.section);
  310. }
  311. if (indexSection.section >= kPagerViewMaxSectionCount-1) {
  312. return TYMakeIndexSection(indexSection.index, kPagerViewMaxSectionCount-1);
  313. }
  314. return TYMakeIndexSection(0, indexSection.section+1);
  315. }
  316. if (indexSection.index > 0) {
  317. return TYMakeIndexSection(indexSection.index-1, indexSection.section);
  318. }
  319. if (indexSection.section <= 0) {
  320. return TYMakeIndexSection(indexSection.index, 0);
  321. }
  322. return TYMakeIndexSection(_numberOfItems-1, indexSection.section-1);
  323. }
  324. - (TYIndexSection)caculateIndexSectionWithOffsetX:(CGPoint)offset {
  325. if (_numberOfItems <= 0) {
  326. return TYMakeIndexSection(0, 0);
  327. }
  328. UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)_collectionView.collectionViewLayout;
  329. if(_layout.scrollDirection == TYCyclePagerScrollDirectionHorizontal){
  330. CGFloat offsetX = offset.x;
  331. CGFloat leftEdge = _isInfiniteLoop ? _layout.sectionInset.left : _layout.onlyOneSectionInset.left;
  332. CGFloat width = CGRectGetWidth(_collectionView.frame);
  333. CGFloat middleOffset = offsetX + width/2;
  334. CGFloat itemWidth = layout.itemSize.width + layout.minimumInteritemSpacing;
  335. NSInteger curIndex = 0;
  336. NSInteger curSection = 0;
  337. if (middleOffset - leftEdge >= 0) {
  338. NSInteger itemIndex = (middleOffset - leftEdge+layout.minimumInteritemSpacing/2)/itemWidth;
  339. if (itemIndex < 0) {
  340. itemIndex = 0;
  341. }else if (itemIndex >= _numberOfItems*kPagerViewMaxSectionCount) {
  342. itemIndex = _numberOfItems*kPagerViewMaxSectionCount-1;
  343. }
  344. curIndex = itemIndex%_numberOfItems;
  345. curSection = itemIndex/_numberOfItems;
  346. }
  347. return TYMakeIndexSection(curIndex, curSection);
  348. }
  349. else{
  350. CGFloat offsetY = offset.y;
  351. CGFloat topEdge = _isInfiniteLoop ? _layout.sectionInset.top : _layout.onlyOneSectionInset.top;
  352. CGFloat height = CGRectGetHeight(_collectionView.frame);
  353. CGFloat middleOffset = offsetY + height/2;
  354. CGFloat itemHeight = layout.itemSize.height + layout.minimumInteritemSpacing;
  355. NSInteger curIndex = 0;
  356. NSInteger curSection = 0;
  357. if (middleOffset - topEdge >= 0) {
  358. NSInteger itemIndex = (middleOffset - topEdge+layout.minimumInteritemSpacing/2)/itemHeight;
  359. if (itemIndex < 0) {
  360. itemIndex = 0;
  361. }else if (itemIndex >= _numberOfItems*kPagerViewMaxSectionCount) {
  362. itemIndex = _numberOfItems*kPagerViewMaxSectionCount-1;
  363. }
  364. curIndex = itemIndex%_numberOfItems;
  365. curSection = itemIndex/_numberOfItems;
  366. }
  367. return TYMakeIndexSection(curIndex, curSection);
  368. }
  369. }
  370. - (CGFloat)caculateOffsetXAtIndexSection:(TYIndexSection)indexSection{
  371. if (_numberOfItems == 0) {
  372. return 0;
  373. }
  374. UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)_collectionView.collectionViewLayout;
  375. UIEdgeInsets edge = _isInfiniteLoop ? _layout.sectionInset : _layout.onlyOneSectionInset;
  376. if(_layout.scrollDirection == TYCyclePagerScrollDirectionHorizontal){
  377. CGFloat leftEdge = edge.left;
  378. CGFloat rightEdge = edge.right;
  379. CGFloat width = CGRectGetWidth(_collectionView.frame);
  380. CGFloat itemWidth = layout.itemSize.width + layout.minimumInteritemSpacing;
  381. CGFloat offsetX = 0;
  382. if (!_isInfiniteLoop && !_layout.itemHorizontalCenter && indexSection.index == _numberOfItems - 1) {
  383. offsetX = leftEdge + itemWidth*(indexSection.index + indexSection.section*_numberOfItems) - (width - itemWidth) - layout.minimumInteritemSpacing + rightEdge;
  384. }else {
  385. offsetX = leftEdge + itemWidth*(indexSection.index + indexSection.section*_numberOfItems) - layout.minimumInteritemSpacing/2 - (width - itemWidth)/2;
  386. }
  387. return MAX(offsetX, 0);
  388. }
  389. else{
  390. CGFloat topEdge = edge.top;
  391. CGFloat bottomEdge = edge.bottom;
  392. CGFloat height = CGRectGetHeight(_collectionView.frame);
  393. CGFloat itemHeight = layout.itemSize.height + layout.minimumInteritemSpacing;
  394. CGFloat offsetY = 0;
  395. if (!_isInfiniteLoop && indexSection.index == _numberOfItems - 1) {
  396. offsetY = topEdge + itemHeight*(indexSection.index + indexSection.section*_numberOfItems) - (height - itemHeight) - layout.minimumInteritemSpacing + bottomEdge;
  397. }else {
  398. offsetY = topEdge + itemHeight*(indexSection.index + indexSection.section*_numberOfItems) - layout.minimumInteritemSpacing/2 - (height - itemHeight)/2;
  399. }
  400. return MAX(offsetY, 0);
  401. }
  402. }
  403. - (void)resetPagerViewAtIndex:(NSInteger)index {
  404. if (_didLayout && _firstScrollIndex >= 0) {
  405. index = _firstScrollIndex;
  406. _firstScrollIndex = -1;
  407. }
  408. if (index < 0) {
  409. return;
  410. }
  411. if (index >= _numberOfItems) {
  412. index = 0;
  413. }
  414. [self scrollToItemAtIndexSection:TYMakeIndexSection(index, _isInfiniteLoop ? kPagerViewMaxSectionCount/3 : 0) animate:NO];
  415. if (!_isInfiniteLoop && _indexSection.index < 0) {
  416. [self scrollViewDidScroll:_collectionView];
  417. }
  418. }
  419. - (void)recyclePagerViewIfNeed {
  420. if (!_isInfiniteLoop) {
  421. return;
  422. }
  423. if (_indexSection.section > kPagerViewMaxSectionCount - kPagerViewMinSectionCount || _indexSection.section < kPagerViewMinSectionCount) {
  424. [self resetPagerViewAtIndex:_indexSection.index];
  425. }
  426. }
  427. #pragma mark - UICollectionViewDataSource
  428. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  429. return _isInfiniteLoop ? kPagerViewMaxSectionCount : 1;
  430. }
  431. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  432. _numberOfItems = [_dataSource numberOfItemsInPagerView:self];
  433. return _numberOfItems;
  434. }
  435. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  436. _dequeueSection = indexPath.section;
  437. if (_dataSourceFlags.cellForItemAtIndex) {
  438. return [_dataSource pagerView:self cellForItemAtIndex:indexPath.row];
  439. }
  440. NSAssert(NO, @"pagerView cellForItemAtIndex: is nil!");
  441. return nil;
  442. }
  443. #pragma mark - UICollectionViewDelegateFlowLayout
  444. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  445. if (!_isInfiniteLoop) {
  446. return _layout.onlyOneSectionInset;
  447. }
  448. if (section == 0 ) {
  449. return _layout.firstSectionInset;
  450. }else if (section == kPagerViewMaxSectionCount -1) {
  451. return _layout.lastSectionInset;
  452. }
  453. return _layout.middleSectionInset;
  454. }
  455. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  456. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  457. if ([_delegate respondsToSelector:@selector(pagerView:didSelectedItemCell:atIndex:)]) {
  458. [_delegate pagerView:self didSelectedItemCell:cell atIndex:indexPath.item];
  459. }
  460. if ([_delegate respondsToSelector:@selector(pagerView:didSelectedItemCell:atIndexSection:)]) {
  461. [_delegate pagerView:self didSelectedItemCell:cell atIndexSection:TYMakeIndexSection(indexPath.item, indexPath.section)];
  462. }
  463. }
  464. #pragma mark - UIScrollViewDelegate
  465. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  466. if (!_didLayout) {
  467. return;
  468. }
  469. TYIndexSection newIndexSection = [self caculateIndexSectionWithOffsetX:scrollView.contentOffset];
  470. if (_numberOfItems <= 0 || ![self isValidIndexSection:newIndexSection]) {
  471. MOLogV(@"inVlaidIndexSection:(%ld,%ld)!",(long)newIndexSection.index,(long)newIndexSection.section);
  472. return;
  473. }
  474. TYIndexSection indexSection = _indexSection;
  475. _indexSection = newIndexSection;
  476. if (_delegateFlags.pagerViewDidScroll) {
  477. [_delegate pagerViewDidScroll:self];
  478. }
  479. if (_delegateFlags.didScrollFromIndexToNewIndex && !TYEqualIndexSection(_indexSection, indexSection)) {
  480. //MOLogV(@"curIndex %ld",(long)_indexSection.index);
  481. [_delegate pagerView:self didScrollFromIndex:MAX(indexSection.index, 0) toIndex:_indexSection.index];
  482. }
  483. }
  484. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  485. if (_autoScrollInterval > 0) {
  486. [self removeTimer];
  487. }
  488. _beginDragIndexSection = [self caculateIndexSectionWithOffsetX:scrollView.contentOffset];
  489. if ([_delegate respondsToSelector:@selector(pagerViewWillBeginDragging:)]) {
  490. [_delegate pagerViewWillBeginDragging:self];
  491. }
  492. }
  493. - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
  494. if(_layout.scrollDirection == TYCyclePagerScrollDirectionHorizontal){
  495. if (fabs(velocity.x) < 0.35 || !TYEqualIndexSection(_beginDragIndexSection, _indexSection)) {
  496. targetContentOffset->x = [self caculateOffsetXAtIndexSection:_indexSection];
  497. return;
  498. }
  499. TYPagerScrollDirection direction = TYPagerScrollDirectionRight;
  500. if ((scrollView.contentOffset.x < 0 && targetContentOffset->x <= 0) || (targetContentOffset->x < scrollView.contentOffset.x && scrollView.contentOffset.x < scrollView.contentSize.width - scrollView.frame.size.width)) {
  501. direction = TYPagerScrollDirectionLeft;
  502. }
  503. TYIndexSection indexSection = [self nearlyIndexPathForIndexSection:_indexSection direction:direction];
  504. targetContentOffset->x = [self caculateOffsetXAtIndexSection:indexSection];
  505. }
  506. else{
  507. if (fabs(velocity.y) < 0.35 || !TYEqualIndexSection(_beginDragIndexSection, _indexSection)) {
  508. targetContentOffset->y = [self caculateOffsetXAtIndexSection:_indexSection];
  509. return;
  510. }
  511. TYPagerScrollDirection direction = TYPagerScrollDirectionRight;
  512. if ((scrollView.contentOffset.y < 0 && targetContentOffset->y <= 0) || (targetContentOffset->y < scrollView.contentOffset.y && scrollView.contentOffset.y < scrollView.contentSize.height - scrollView.frame.size.height)) {
  513. direction = TYPagerScrollDirectionLeft;
  514. }
  515. TYIndexSection indexSection = [self nearlyIndexPathForIndexSection:_indexSection direction:direction];
  516. targetContentOffset->y = [self caculateOffsetXAtIndexSection:indexSection];
  517. }
  518. }
  519. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  520. if (_autoScrollInterval > 0) {
  521. [self addTimer];
  522. }
  523. if ([_delegate respondsToSelector:@selector(pagerViewDidEndDragging:willDecelerate:)]) {
  524. [_delegate pagerViewDidEndDragging:self willDecelerate:decelerate];
  525. }
  526. }
  527. - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
  528. if ([_delegate respondsToSelector:@selector(pagerViewWillBeginDecelerating:)]) {
  529. [_delegate pagerViewWillBeginDecelerating:self];
  530. }
  531. }
  532. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  533. [self recyclePagerViewIfNeed];
  534. if ([_delegate respondsToSelector:@selector(pagerViewDidEndDecelerating:)]) {
  535. [_delegate pagerViewDidEndDecelerating:self];
  536. }
  537. }
  538. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
  539. [self recyclePagerViewIfNeed];
  540. if ([_delegate respondsToSelector:@selector(pagerViewDidEndScrollingAnimation:)]) {
  541. [_delegate pagerViewDidEndScrollingAnimation:self];
  542. }
  543. }
  544. #pragma mark - TYCyclePagerTransformLayoutDelegate
  545. - (void)pagerViewTransformLayout:(TYCyclePagerTransformLayout *)pagerViewTransformLayout initializeTransformAttributes:(UICollectionViewLayoutAttributes *)attributes {
  546. if (_delegateFlags.initializeTransformAttributes) {
  547. [_delegate pagerView:self initializeTransformAttributes:attributes];
  548. }
  549. }
  550. - (void)pagerViewTransformLayout:(TYCyclePagerTransformLayout *)pagerViewTransformLayout applyTransformToAttributes:(UICollectionViewLayoutAttributes *)attributes {
  551. if (_delegateFlags.applyTransformToAttributes) {
  552. [_delegate pagerView:self applyTransformToAttributes:attributes];
  553. }
  554. }
  555. - (void)layoutSubviews {
  556. [super layoutSubviews];
  557. BOOL needUpdateLayout = !CGRectEqualToRect(_collectionView.frame, self.bounds);
  558. _collectionView.frame = self.bounds;
  559. if ((_indexSection.section < 0 || needUpdateLayout) && (_numberOfItems > 0 || _didReloadData)) {
  560. _didLayout = YES;
  561. [self setNeedUpdateLayout];
  562. }
  563. }
  564. - (void)dealloc {
  565. ((TYCyclePagerTransformLayout *)_collectionView.collectionViewLayout).delegate = nil;
  566. _collectionView.delegate = nil;
  567. _collectionView.dataSource = nil;
  568. }
  569. @end