TYPagerViewLayout.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // TYPagerViewLayout.h
  3. // TYPagerControllerDemo
  4. //
  5. // Created by tanyang on 2017/7/9.
  6. // Copyright © 2017年 tany. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. @interface NSObject (TY_PagerReuseIdentify)
  11. // resueId
  12. @property (nonatomic, strong, readonly, nullable) NSString *ty_pagerReuseIdentify;
  13. @end
  14. @class TYPagerViewLayout<ItemType>;
  15. @protocol TYPagerViewLayoutDataSource <NSObject>
  16. - (NSInteger)numberOfItemsInPagerViewLayout;
  17. // if item is preload, prefetch will YES
  18. - (id)pagerViewLayout:(TYPagerViewLayout *)pagerViewLayout itemForIndex:(NSInteger)index prefetching:(BOOL)prefetching;
  19. // return item's view
  20. - (UIView *)pagerViewLayout:(TYPagerViewLayout *)pagerViewLayout viewForItem:(id)item atIndex:(NSInteger)index;
  21. // see TYPagerView&&TYPagerController, add&&remove item ,must implement scrollView addSubView item's view
  22. - (void)pagerViewLayout:(TYPagerViewLayout *)pagerViewLayout addVisibleItem:(id)item atIndex:(NSInteger)index;
  23. - (void)pagerViewLayout:(TYPagerViewLayout *)pagerViewLayout removeInVisibleItem:(id)item atIndex:(NSInteger)index;
  24. @optional
  25. // if have not viewController return nil.
  26. - (UIViewController *)pagerViewLayout:(TYPagerViewLayout *)pagerViewLayout viewControllerForItem:(id)item atIndex:(NSInteger)index;
  27. @end
  28. @protocol TYPagerViewLayoutDelegate <NSObject>
  29. @optional
  30. // Transition animation customization
  31. // if you implement ↓↓↓transitionFromIndex:toIndex:progress:,only tap change index will call this, you can set progressAnimateEnabel NO that not call progress method
  32. - (void)pagerViewLayout:(TYPagerViewLayout *)pagerViewLayout transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated;
  33. // if you implement the method,also you need implement ↑↑↑transitionFromIndex:toIndex:animated:,deal with tap change index animate
  34. - (void)pagerViewLayout:(TYPagerViewLayout *)pagerViewLayout transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress;
  35. // ScrollViewDelegate
  36. - (void)pagerViewLayoutDidScroll:(TYPagerViewLayout *)pagerViewLayout;
  37. - (void)pagerViewLayoutWillBeginScrollToView:(TYPagerViewLayout *)pagerViewLayout animate:(BOOL)animate;
  38. - (void)pagerViewLayoutDidEndScrollToView:(TYPagerViewLayout *)pagerViewLayout animate:(BOOL)animate;
  39. - (void)pagerViewLayoutWillBeginDragging:(TYPagerViewLayout *)pagerViewLayout;
  40. - (void)pagerViewLayoutDidEndDragging:(TYPagerViewLayout *)pagerViewLayout willDecelerate:(BOOL)decelerate;
  41. - (void)pagerViewLayoutWillBeginDecelerating:(TYPagerViewLayout *)pagerViewLayout;
  42. - (void)pagerViewLayoutDidEndDecelerating:(TYPagerViewLayout *)pagerViewLayout;
  43. - (void)pagerViewLayoutDidEndScrollingAnimation:(TYPagerViewLayout *)pagerViewLayout;
  44. @end
  45. @interface TYPagerViewLayout<__covariant ItemType> : NSObject
  46. @property (nonatomic, weak, nullable) id<TYPagerViewLayoutDataSource> dataSource;
  47. @property (nonatomic, weak, nullable) id<TYPagerViewLayoutDelegate> delegate;
  48. // strong,will control the delegate,don't set delegate on other place.
  49. @property (nonatomic, strong, readonly) UIScrollView *scrollView;
  50. // if viewcontroller's automaticallyAdjustsScrollViewInsets YES ,will cause frame problems, you can set YES, default YES
  51. @property (nonatomic, assign) BOOL adjustScrollViewInset;
  52. @property (nonatomic, assign, readonly) NSInteger countOfPagerItems;
  53. @property (nonatomic, assign, readonly) NSInteger curIndex;// default -1
  54. @property (nonatomic, strong, readonly) NSCache<NSNumber *,ItemType> *memoryCache;; // will cache pagerView,you can set countLimit
  55. @property (nonatomic, assign) BOOL autoMemoryCache; // default YES
  56. @property (nonatomic, assign) NSInteger prefetchItemCount;// preload left and right item's count , default 0
  57. // because when superview add subview(have tableView) will call relodData,if set Yes will optimize. default NO
  58. @property (nonatomic, assign) BOOL prefetchItemWillAddToSuperView;
  59. @property (nonatomic, assign, readonly) NSRange prefetchRange;
  60. @property (nonatomic, assign, readonly) NSRange visibleRange;
  61. @property (nonatomic, strong, nullable, readonly) NSArray<NSNumber *> * visibleIndexs;
  62. @property (nonatomic, strong, nullable, readonly) NSArray<ItemType> * visibleItems;
  63. // default YES, if NO,will not call delegate transitionFromIndex:toIndex:progress:,but will call transitionFromIndex:toIndex:
  64. @property (nonatomic, assign) BOOL progressAnimateEnabel;
  65. // default NO, when scroll visible range change will add item.If YES add item only when scroll animate end, suggest set prefetchItemCount 1 or more
  66. @property (nonatomic, assign) BOOL addVisibleItemOnlyWhenScrollAnimatedEnd;
  67. // default 0.5,when scroll progress percent will change index, only progressAnimateEnabel is NO or don't implement delegate transitionFromIndex: toIndex: progress:
  68. @property (nonatomic, assign) CGFloat changeIndexWhenScrollProgress;
  69. - (instancetype)init NS_UNAVAILABLE;
  70. + (instancetype)new NS_UNAVAILABLE;
  71. /**
  72. initializer will strong scrollView,and control delegate,don't set delegate on other place.
  73. */
  74. - (instancetype)initWithScrollView:(UIScrollView *)scrollView NS_DESIGNATED_INITIALIZER; // strong scrollView
  75. - (ItemType _Nullable)itemForIndex:(NSInteger)idx;
  76. - (UIView *)viewForItem:(ItemType)item atIndex:(NSInteger)index;
  77. // if have not viewController return nil.
  78. - (UIViewController *_Nullable)viewControllerForItem:(id)item atIndex:(NSInteger)index;
  79. // view's frame at index
  80. - (CGRect)frameForItemAtIndex:(NSInteger)index;
  81. // register && dequeue's usage like tableView
  82. - (void)registerClass:(Class)Class forItemWithReuseIdentifier:(NSString *)identifier;
  83. - (void)registerNib:(UINib *)nib forItemWithReuseIdentifier:(NSString *)identifier;
  84. - (ItemType)dequeueReusableItemWithReuseIdentifier:(NSString *)identifier forIndex:(NSInteger)index;
  85. // scroll to index
  86. - (void)scrollToItemAtIndex:(NSInteger)index animate:(BOOL)animate;
  87. // update data and layout,the same to relaodData,but don't reset propertys(curIndex,visibleDatas,prefechDatas)
  88. - (void)updateData;
  89. // reload data and reset propertys
  90. - (void)reloadData;
  91. @end
  92. NS_ASSUME_NONNULL_END