TYCyclePagerView.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. - (void)addTimer;
  93. - (void)removeTimer;
  94. /**
  95. will set layout nil and call delegate->layoutForPagerView
  96. */
  97. - (void)setNeedClearLayout;
  98. /**
  99. current index cell in pagerView
  100. */
  101. - (__kindof UICollectionViewCell * _Nullable)curIndexCell;
  102. /**
  103. visible cells in pageView
  104. */
  105. - (NSArray<__kindof UICollectionViewCell *> *_Nullable)visibleCells;
  106. /**
  107. visible pageView indexs, maybe repeat index
  108. */
  109. - (NSArray *)visibleIndexs;
  110. /**
  111. scroll to item at index
  112. */
  113. - (void)scrollToItemAtIndex:(NSInteger)index animate:(BOOL)animate;
  114. - (void)scrollToItemAtIndexSection:(TYIndexSection)indexSection animate:(BOOL)animate;
  115. /**
  116. scroll to next or pre item
  117. */
  118. - (void)scrollToNearlyIndexAtDirection:(TYPagerScrollDirection)direction animate:(BOOL)animate;
  119. /**
  120. register pager view cell with class
  121. */
  122. - (void)registerClass:(Class)Class forCellWithReuseIdentifier:(NSString *)identifier;
  123. /**
  124. register pager view cell with nib
  125. */
  126. - (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
  127. /**
  128. dequeue reusable cell for pagerView
  129. */
  130. - (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndex:(NSInteger)index;
  131. @end
  132. NS_ASSUME_NONNULL_END