TYCyclePagerView.m 25 KB

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