TYCyclePagerView.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 caculateOffsetXAtIndexSection:indexSection];
  243. [_collectionView setContentOffset:CGPointMake(offset, _collectionView.contentOffset.y) animated:animate];
  244. }
  245. - (void)registerClass:(Class)Class forCellWithReuseIdentifier:(NSString *)identifier {
  246. [_collectionView registerClass:Class forCellWithReuseIdentifier:identifier];
  247. }
  248. - (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier {
  249. [_collectionView registerNib:nib forCellWithReuseIdentifier:identifier];
  250. }
  251. - (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndex:(NSInteger)index {
  252. UICollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:[NSIndexPath indexPathForItem:index inSection:_dequeueSection]];
  253. return cell;
  254. }
  255. #pragma mark - configure layout
  256. - (void)updateLayout {
  257. if (!self.layout) {
  258. return;
  259. }
  260. self.layout.isInfiniteLoop = _isInfiniteLoop;
  261. ((TYCyclePagerTransformLayout *)_collectionView.collectionViewLayout).layout = self.layout;
  262. }
  263. - (void)clearLayout {
  264. if (_needClearLayout) {
  265. _layout = nil;
  266. _needClearLayout = NO;
  267. }
  268. }
  269. - (void)setNeedClearLayout {
  270. _needClearLayout = YES;
  271. }
  272. - (void)setNeedUpdateLayout {
  273. if (!self.layout) {
  274. return;
  275. }
  276. [self clearLayout];
  277. [self updateLayout];
  278. [_collectionView.collectionViewLayout invalidateLayout];
  279. [self resetPagerViewAtIndex:_indexSection.index < 0 ? 0 :_indexSection.index];
  280. }
  281. #pragma mark - pager index
  282. - (BOOL)isValidIndexSection:(TYIndexSection)indexSection {
  283. return indexSection.index >= 0 && indexSection.index < _numberOfItems && indexSection.section >= 0 && indexSection.section < kPagerViewMaxSectionCount;
  284. }
  285. - (TYIndexSection)nearlyIndexPathAtDirection:(TYPagerScrollDirection)direction{
  286. return [self nearlyIndexPathForIndexSection:_indexSection direction:direction];
  287. }
  288. - (TYIndexSection)nearlyIndexPathForIndexSection:(TYIndexSection)indexSection direction:(TYPagerScrollDirection)direction {
  289. if (indexSection.index < 0 || indexSection.index >= _numberOfItems) {
  290. return indexSection;
  291. }
  292. if (!_isInfiniteLoop) {
  293. if (direction == TYPagerScrollDirectionRight && indexSection.index == _numberOfItems - 1) {
  294. return _autoScrollInterval > 0 ? TYMakeIndexSection(0, 0) : indexSection;
  295. } else if (direction == TYPagerScrollDirectionRight) {
  296. return TYMakeIndexSection(indexSection.index+1, 0);
  297. }
  298. if (indexSection.index == 0) {
  299. return _autoScrollInterval > 0 ? TYMakeIndexSection(_numberOfItems - 1, 0) : indexSection;
  300. }
  301. return TYMakeIndexSection(indexSection.index-1, 0);
  302. }
  303. if (direction == TYPagerScrollDirectionRight) {
  304. if (indexSection.index < _numberOfItems-1) {
  305. return TYMakeIndexSection(indexSection.index+1, indexSection.section);
  306. }
  307. if (indexSection.section >= kPagerViewMaxSectionCount-1) {
  308. return TYMakeIndexSection(indexSection.index, kPagerViewMaxSectionCount-1);
  309. }
  310. return TYMakeIndexSection(0, indexSection.section+1);
  311. }
  312. if (indexSection.index > 0) {
  313. return TYMakeIndexSection(indexSection.index-1, indexSection.section);
  314. }
  315. if (indexSection.section <= 0) {
  316. return TYMakeIndexSection(indexSection.index, 0);
  317. }
  318. return TYMakeIndexSection(_numberOfItems-1, indexSection.section-1);
  319. }
  320. - (TYIndexSection)caculateIndexSectionWithOffsetX:(CGFloat)offsetX {
  321. if (_numberOfItems <= 0) {
  322. return TYMakeIndexSection(0, 0);
  323. }
  324. UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)_collectionView.collectionViewLayout;
  325. CGFloat leftEdge = _isInfiniteLoop ? _layout.sectionInset.left : _layout.onlyOneSectionInset.left;
  326. CGFloat width = CGRectGetWidth(_collectionView.frame);
  327. CGFloat middleOffset = offsetX + width/2;
  328. CGFloat itemWidth = layout.itemSize.width + layout.minimumInteritemSpacing;
  329. NSInteger curIndex = 0;
  330. NSInteger curSection = 0;
  331. if (middleOffset - leftEdge >= 0) {
  332. NSInteger itemIndex = (middleOffset - leftEdge+layout.minimumInteritemSpacing/2)/itemWidth;
  333. if (itemIndex < 0) {
  334. itemIndex = 0;
  335. }else if (itemIndex >= _numberOfItems*kPagerViewMaxSectionCount) {
  336. itemIndex = _numberOfItems*kPagerViewMaxSectionCount-1;
  337. }
  338. curIndex = itemIndex%_numberOfItems;
  339. curSection = itemIndex/_numberOfItems;
  340. }
  341. return TYMakeIndexSection(curIndex, curSection);
  342. }
  343. - (CGFloat)caculateOffsetXAtIndexSection:(TYIndexSection)indexSection{
  344. if (_numberOfItems == 0) {
  345. return 0;
  346. }
  347. UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)_collectionView.collectionViewLayout;
  348. UIEdgeInsets edge = _isInfiniteLoop ? _layout.sectionInset : _layout.onlyOneSectionInset;
  349. CGFloat leftEdge = edge.left;
  350. CGFloat rightEdge = edge.right;
  351. CGFloat width = CGRectGetWidth(_collectionView.frame);
  352. CGFloat itemWidth = layout.itemSize.width + layout.minimumInteritemSpacing;
  353. CGFloat offsetX = 0;
  354. if (!_isInfiniteLoop && !_layout.itemHorizontalCenter && indexSection.index == _numberOfItems - 1) {
  355. offsetX = leftEdge + itemWidth*(indexSection.index + indexSection.section*_numberOfItems) - (width - itemWidth) - layout.minimumInteritemSpacing + rightEdge;
  356. }else {
  357. offsetX = leftEdge + itemWidth*(indexSection.index + indexSection.section*_numberOfItems) - layout.minimumInteritemSpacing/2 - (width - itemWidth)/2;
  358. }
  359. return MAX(offsetX, 0);
  360. }
  361. - (void)resetPagerViewAtIndex:(NSInteger)index {
  362. if (_didLayout && _firstScrollIndex >= 0) {
  363. index = _firstScrollIndex;
  364. _firstScrollIndex = -1;
  365. }
  366. if (index < 0) {
  367. return;
  368. }
  369. if (index >= _numberOfItems) {
  370. index = 0;
  371. }
  372. [self scrollToItemAtIndexSection:TYMakeIndexSection(index, _isInfiniteLoop ? kPagerViewMaxSectionCount/3 : 0) animate:NO];
  373. if (!_isInfiniteLoop && _indexSection.index < 0) {
  374. [self scrollViewDidScroll:_collectionView];
  375. }
  376. }
  377. - (void)recyclePagerViewIfNeed {
  378. if (!_isInfiniteLoop) {
  379. return;
  380. }
  381. if (_indexSection.section > kPagerViewMaxSectionCount - kPagerViewMinSectionCount || _indexSection.section < kPagerViewMinSectionCount) {
  382. [self resetPagerViewAtIndex:_indexSection.index];
  383. }
  384. }
  385. #pragma mark - UICollectionViewDataSource
  386. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  387. return _isInfiniteLoop ? kPagerViewMaxSectionCount : 1;
  388. }
  389. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  390. _numberOfItems = [_dataSource numberOfItemsInPagerView:self];
  391. return _numberOfItems;
  392. }
  393. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  394. _dequeueSection = indexPath.section;
  395. if (_dataSourceFlags.cellForItemAtIndex) {
  396. return [_dataSource pagerView:self cellForItemAtIndex:indexPath.row];
  397. }
  398. NSAssert(NO, @"pagerView cellForItemAtIndex: is nil!");
  399. return nil;
  400. }
  401. #pragma mark - UICollectionViewDelegateFlowLayout
  402. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  403. if (!_isInfiniteLoop) {
  404. return _layout.onlyOneSectionInset;
  405. }
  406. if (section == 0 ) {
  407. return _layout.firstSectionInset;
  408. }else if (section == kPagerViewMaxSectionCount -1) {
  409. return _layout.lastSectionInset;
  410. }
  411. return _layout.middleSectionInset;
  412. }
  413. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  414. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  415. if ([_delegate respondsToSelector:@selector(pagerView:didSelectedItemCell:atIndex:)]) {
  416. [_delegate pagerView:self didSelectedItemCell:cell atIndex:indexPath.item];
  417. }
  418. if ([_delegate respondsToSelector:@selector(pagerView:didSelectedItemCell:atIndexSection:)]) {
  419. [_delegate pagerView:self didSelectedItemCell:cell atIndexSection:TYMakeIndexSection(indexPath.item, indexPath.section)];
  420. }
  421. }
  422. #pragma mark - UIScrollViewDelegate
  423. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  424. if (!_didLayout) {
  425. return;
  426. }
  427. TYIndexSection newIndexSection = [self caculateIndexSectionWithOffsetX:scrollView.contentOffset.x];
  428. if (_numberOfItems <= 0 || ![self isValidIndexSection:newIndexSection]) {
  429. NSLog(@"inVlaidIndexSection:(%ld,%ld)!",(long)newIndexSection.index,(long)newIndexSection.section);
  430. return;
  431. }
  432. TYIndexSection indexSection = _indexSection;
  433. _indexSection = newIndexSection;
  434. if (_delegateFlags.pagerViewDidScroll) {
  435. [_delegate pagerViewDidScroll:self];
  436. }
  437. if (_delegateFlags.didScrollFromIndexToNewIndex && !TYEqualIndexSection(_indexSection, indexSection)) {
  438. //NSLog(@"curIndex %ld",(long)_indexSection.index);
  439. [_delegate pagerView:self didScrollFromIndex:MAX(indexSection.index, 0) toIndex:_indexSection.index];
  440. }
  441. }
  442. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  443. if (_autoScrollInterval > 0) {
  444. [self removeTimer];
  445. }
  446. _beginDragIndexSection = [self caculateIndexSectionWithOffsetX:scrollView.contentOffset.x];
  447. if ([_delegate respondsToSelector:@selector(pagerViewWillBeginDragging:)]) {
  448. [_delegate pagerViewWillBeginDragging:self];
  449. }
  450. }
  451. - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
  452. if (fabs(velocity.x) < 0.35 || !TYEqualIndexSection(_beginDragIndexSection, _indexSection)) {
  453. targetContentOffset->x = [self caculateOffsetXAtIndexSection:_indexSection];
  454. return;
  455. }
  456. TYPagerScrollDirection direction = TYPagerScrollDirectionRight;
  457. if ((scrollView.contentOffset.x < 0 && targetContentOffset->x <= 0) || (targetContentOffset->x < scrollView.contentOffset.x && scrollView.contentOffset.x < scrollView.contentSize.width - scrollView.frame.size.width)) {
  458. direction = TYPagerScrollDirectionLeft;
  459. }
  460. TYIndexSection indexSection = [self nearlyIndexPathForIndexSection:_indexSection direction:direction];
  461. targetContentOffset->x = [self caculateOffsetXAtIndexSection:indexSection];
  462. }
  463. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  464. if (_autoScrollInterval > 0) {
  465. [self addTimer];
  466. }
  467. if ([_delegate respondsToSelector:@selector(pagerViewDidEndDragging:willDecelerate:)]) {
  468. [_delegate pagerViewDidEndDragging:self willDecelerate:decelerate];
  469. }
  470. }
  471. - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
  472. if ([_delegate respondsToSelector:@selector(pagerViewWillBeginDecelerating:)]) {
  473. [_delegate pagerViewWillBeginDecelerating:self];
  474. }
  475. }
  476. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  477. [self recyclePagerViewIfNeed];
  478. if ([_delegate respondsToSelector:@selector(pagerViewDidEndDecelerating:)]) {
  479. [_delegate pagerViewDidEndDecelerating:self];
  480. }
  481. }
  482. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
  483. [self recyclePagerViewIfNeed];
  484. if ([_delegate respondsToSelector:@selector(pagerViewDidEndScrollingAnimation:)]) {
  485. [_delegate pagerViewDidEndScrollingAnimation:self];
  486. }
  487. }
  488. #pragma mark - TYCyclePagerTransformLayoutDelegate
  489. - (void)pagerViewTransformLayout:(TYCyclePagerTransformLayout *)pagerViewTransformLayout initializeTransformAttributes:(UICollectionViewLayoutAttributes *)attributes {
  490. if (_delegateFlags.initializeTransformAttributes) {
  491. [_delegate pagerView:self initializeTransformAttributes:attributes];
  492. }
  493. }
  494. - (void)pagerViewTransformLayout:(TYCyclePagerTransformLayout *)pagerViewTransformLayout applyTransformToAttributes:(UICollectionViewLayoutAttributes *)attributes {
  495. if (_delegateFlags.applyTransformToAttributes) {
  496. [_delegate pagerView:self applyTransformToAttributes:attributes];
  497. }
  498. }
  499. - (void)layoutSubviews {
  500. [super layoutSubviews];
  501. BOOL needUpdateLayout = !CGRectEqualToRect(_collectionView.frame, self.bounds);
  502. _collectionView.frame = self.bounds;
  503. if ((_indexSection.section < 0 || needUpdateLayout) && (_numberOfItems > 0 || _didReloadData)) {
  504. _didLayout = YES;
  505. [self setNeedUpdateLayout];
  506. }
  507. }
  508. - (void)dealloc {
  509. ((TYCyclePagerTransformLayout *)_collectionView.collectionViewLayout).delegate = nil;
  510. _collectionView.delegate = nil;
  511. _collectionView.dataSource = nil;
  512. }
  513. @end