TYCyclePagerView.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // TYCyclePagerView.h
  3. // TYCyclePagerViewDemo
  4. //
  5. // Created by tany on 2017/6/14.
  6. // Copyright © 2017年 tany. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "TYCyclePagerTransformLayout.h"
  10. NS_ASSUME_NONNULL_BEGIN
  11. typedef struct {
  12. NSInteger index;
  13. NSInteger section;
  14. }TYIndexSection;
  15. // pagerView scrolling direction
  16. typedef NS_ENUM(NSUInteger, TYPagerScrollDirection) {
  17. TYPagerScrollDirectionLeft,
  18. TYPagerScrollDirectionRight,
  19. };
  20. @class TYCyclePagerView;
  21. @protocol TYCyclePagerViewDataSource <NSObject>
  22. - (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView;
  23. - (__kindof UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index;
  24. /**
  25. return pagerView layout,and cache layout
  26. */
  27. - (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView;
  28. @end
  29. @protocol TYCyclePagerViewDelegate <NSObject>
  30. @optional
  31. /**
  32. pagerView did scroll to new index page
  33. */
  34. - (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex;
  35. /**
  36. pagerView did selected item cell
  37. */
  38. - (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index;
  39. - (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndexSection:(TYIndexSection)indexSection;
  40. // custom layout
  41. - (void)pagerView:(TYCyclePagerView *)pageView initializeTransformAttributes:(UICollectionViewLayoutAttributes *)attributes;
  42. - (void)pagerView:(TYCyclePagerView *)pageView applyTransformToAttributes:(UICollectionViewLayoutAttributes *)attributes;
  43. // scrollViewDelegate
  44. - (void)pagerViewDidScroll:(TYCyclePagerView *)pageView;
  45. - (void)pagerViewWillBeginDragging:(TYCyclePagerView *)pageView;
  46. - (void)pagerViewDidEndDragging:(TYCyclePagerView *)pageView willDecelerate:(BOOL)decelerate;
  47. - (void)pagerViewWillBeginDecelerating:(TYCyclePagerView *)pageView;
  48. - (void)pagerViewDidEndDecelerating:(TYCyclePagerView *)pageView;
  49. - (void)pagerViewWillBeginScrollingAnimation:(TYCyclePagerView *)pageView;
  50. - (void)pagerViewDidEndScrollingAnimation:(TYCyclePagerView *)pageView;
  51. @end
  52. @interface TYCyclePagerView : UIView
  53. // will be automatically resized to track the size of the pagerView
  54. @property (nonatomic, strong, nullable) UIView *backgroundView;
  55. @property (nonatomic, weak, nullable) id<TYCyclePagerViewDataSource> dataSource;
  56. @property (nonatomic, weak, nullable) id<TYCyclePagerViewDelegate> delegate;
  57. // pager view, don't set dataSource and delegate
  58. @property (nonatomic, weak, readonly) UICollectionView *collectionView;
  59. // pager view layout
  60. @property (nonatomic, strong, readonly) TYCyclePagerViewLayout *layout;
  61. /**
  62. is infinite cycle pageview
  63. */
  64. @property (nonatomic, assign) BOOL isInfiniteLoop;
  65. /**
  66. pagerView automatic scroll time interval, default 0,disable automatic
  67. */
  68. @property (nonatomic, assign) CGFloat autoScrollInterval;
  69. @property (nonatomic, assign) BOOL reloadDataNeedResetIndex;
  70. /**
  71. current page index
  72. */
  73. @property (nonatomic, assign, readonly) NSInteger curIndex;
  74. @property (nonatomic, assign, readonly) TYIndexSection indexSection;
  75. // scrollView property
  76. @property (nonatomic, assign, readonly) CGPoint contentOffset;
  77. @property (nonatomic, assign, readonly) BOOL tracking;
  78. @property (nonatomic, assign, readonly) BOOL dragging;
  79. @property (nonatomic, assign, readonly) BOOL decelerating;
  80. /**
  81. reload data, !!important!!: will clear layout and call delegate layoutForPagerView
  82. */
  83. - (void)reloadData;
  84. /**
  85. update data is reload data, but not clear layuot
  86. */
  87. - (void)updateData;
  88. /**
  89. if you only want update layout
  90. */
  91. - (void)setNeedUpdateLayout;
  92. /**
  93. will set layout nil and call delegate->layoutForPagerView
  94. */
  95. - (void)setNeedClearLayout;
  96. /**
  97. current index cell in pagerView
  98. */
  99. - (__kindof UICollectionViewCell * _Nullable)curIndexCell;
  100. /**
  101. visible cells in pageView
  102. */
  103. - (NSArray<__kindof UICollectionViewCell *> *_Nullable)visibleCells;
  104. /**
  105. visible pageView indexs, maybe repeat index
  106. */
  107. - (NSArray *)visibleIndexs;
  108. /**
  109. scroll to item at index
  110. */
  111. - (void)scrollToItemAtIndex:(NSInteger)index animate:(BOOL)animate;
  112. - (void)scrollToItemAtIndexSection:(TYIndexSection)indexSection animate:(BOOL)animate;
  113. /**
  114. scroll to next or pre item
  115. */
  116. - (void)scrollToNearlyIndexAtDirection:(TYPagerScrollDirection)direction animate:(BOOL)animate;
  117. /**
  118. register pager view cell with class
  119. */
  120. - (void)registerClass:(Class)Class forCellWithReuseIdentifier:(NSString *)identifier;
  121. /**
  122. register pager view cell with nib
  123. */
  124. - (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
  125. /**
  126. dequeue reusable cell for pagerView
  127. */
  128. - (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndex:(NSInteger)index;
  129. @end
  130. NS_ASSUME_NONNULL_END