暂无描述

WMPageController.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // WMPageController.h
  3. // WMPageController
  4. //
  5. // Created by Mark on 15/6/11.
  6. // Copyright (c) 2015年 yq. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "WMMenuView.h"
  10. #import "WMScrollView.h"
  11. @class WMPageController;
  12. /*
  13. * WMPageController 的缓存设置,默认缓存为无限制,当收到 memoryWarning 时,会自动切换到低缓存模式 (WMPageControllerCachePolicyLowMemory),并在一段时间后切换到 High .
  14. 收到多次警告后,会停留在到 WMPageControllerCachePolicyLowMemory 不再增长
  15. *
  16. * The Default cache policy is No Limit, when recieved memory warning, page controller will switch mode to 'LowMemory'
  17. and continue to grow back after a while.
  18. If recieved too much times, the cache policy will stay at 'LowMemory' and don't grow back any more.
  19. */
  20. typedef NS_ENUM(NSInteger, WMPageControllerCachePolicy) {
  21. WMPageControllerCachePolicyDisabled = -1, // Disable Cache
  22. WMPageControllerCachePolicyNoLimit = 0, // No limit
  23. WMPageControllerCachePolicyLowMemory = 1, // Low Memory but may block when scroll
  24. WMPageControllerCachePolicyBalanced = 3, // Balanced ↑ and ↓
  25. WMPageControllerCachePolicyHigh = 5 // High
  26. };
  27. typedef NS_ENUM(NSUInteger, WMPageControllerPreloadPolicy) {
  28. WMPageControllerPreloadPolicyNever = 0, // Never pre-load controller.
  29. WMPageControllerPreloadPolicyNeighbour = 1, // Pre-load the controller next to the current.
  30. WMPageControllerPreloadPolicyNear = 2 // Pre-load 2 controllers near the current.
  31. };
  32. NS_ASSUME_NONNULL_BEGIN
  33. extern NSString *const WMControllerDidAddToSuperViewNotification;
  34. extern NSString *const WMControllerDidFullyDisplayedNotification;
  35. @protocol WMPageControllerDataSource <NSObject>
  36. @optional
  37. /**
  38. * To inform how many child controllers will in `WMPageController`.
  39. *
  40. * @param pageController The parent controller.
  41. *
  42. * @return The value of child controllers's count.
  43. */
  44. - (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController;
  45. /**
  46. * Return a controller that you wanna to display at index. You can set properties easily if you implement this methods.
  47. *
  48. * @param pageController The parent controller.
  49. * @param index The index of child controller.
  50. *
  51. * @return The instance of a `UIViewController`.
  52. */
  53. - (__kindof UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index;
  54. /**
  55. * Each title you wanna show in the `WMMenuView`
  56. *
  57. * @param pageController The parent controller.
  58. * @param index The index of title.
  59. *
  60. * @return A `NSString` value to show at the top of `WMPageController`.
  61. */
  62. - (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index;
  63. @required
  64. /**
  65. Implement this datasource method, in order to customize your own contentView's frame
  66. @param pageController The container controller
  67. @param contentView The contentView, each is the superview of the child controllers
  68. @return The frame of the contentView
  69. */
  70. - (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView;
  71. /**
  72. Implement this datasource method, in order to customize your own menuView's frame
  73. @param pageController The container controller
  74. @param menuView The menuView
  75. @return The frame of the menuView
  76. */
  77. - (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView;
  78. @end
  79. @protocol WMPageControllerDelegate <NSObject>
  80. @optional
  81. /**
  82. * If the child controller is heavy, put some work in this method. This method will only be called when the controller is initialized and stop scrolling. (That means if the controller is cached and hasn't released will never call this method.)
  83. *
  84. * @param pageController The parent controller (WMPageController)
  85. * @param viewController The viewController first show up when scroll stop.
  86. * @param info A dictionary that includes some infos, such as: `index` / `title`
  87. */
  88. - (void)pageController:(WMPageController *)pageController lazyLoadViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info;
  89. /**
  90. * Called when a viewController will be cached. You can clear some data if it's not reusable.
  91. *
  92. * @param pageController The parent controller (WMPageController)
  93. * @param viewController The viewController will be cached.
  94. * @param info A dictionary that includes some infos, such as: `index` / `title`
  95. */
  96. - (void)pageController:(WMPageController *)pageController willCachedViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info;
  97. /**
  98. * Called when a viewController will be appear to user's sight. Do some preparatory methods if needed.
  99. *
  100. * @param pageController The parent controller (WMPageController)
  101. * @param viewController The viewController will appear.
  102. * @param info A dictionary that includes some infos, such as: `index` / `title`
  103. */
  104. - (void)pageController:(WMPageController *)pageController willEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info;
  105. /**
  106. * Called when a viewController will fully displayed, that means, scrollView have stopped scrolling and the controller's view have entirely displayed.
  107. *
  108. * @param pageController The parent controller (WMPageController)
  109. * @param viewController The viewController entirely displayed.
  110. * @param info A dictionary that includes some infos, such as: `index` / `title`
  111. */
  112. - (void)pageController:(WMPageController *)pageController didEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info;
  113. @end
  114. @interface WMPageController : UIViewController <WMMenuViewDelegate, WMMenuViewDataSource, UIScrollViewDelegate, WMPageControllerDataSource, WMPageControllerDelegate>
  115. @property (nonatomic, weak) id<WMPageControllerDelegate> delegate;
  116. @property (nonatomic, weak) id<WMPageControllerDataSource> dataSource;
  117. /**
  118. * Values and keys can set properties when initialize child controlelr (it's KVC)
  119. * values keys 属性可以用于初始化控制器的时候为控制器传值(利用 KVC 来设置)
  120. 使用时请确保 key 与控制器的属性名字一致!!(例如:控制器有需要设置的属性 type,那么 keys 所放的就是字符串 @"type")
  121. */
  122. @property (nonatomic, nullable, strong) NSMutableArray<id> *values;
  123. @property (nonatomic, nullable, strong) NSMutableArray<NSString *> *keys;
  124. /**
  125. * 各个控制器的 class, 例如:[UITableViewController class]
  126. * Each controller's class, example:[UITableViewController class]
  127. */
  128. @property (nonatomic, nullable, copy) NSArray<Class> *viewControllerClasses;
  129. /**
  130. * 各个控制器标题
  131. * Titles of view controllers in page controller.
  132. */
  133. @property (nonatomic, nullable, copy) NSArray<NSString *> *titles;
  134. @property (nonatomic, strong, readonly) UIViewController *currentViewController;
  135. /**
  136. * 设置选中几号 item
  137. * To select item at index
  138. */
  139. @property (nonatomic, assign) int selectIndex;
  140. /**
  141. * 点击的 MenuItem 是否触发滚动动画
  142. * Whether to animate when press the MenuItem
  143. */
  144. @property (nonatomic, assign) BOOL pageAnimatable;
  145. /** 是否自动通过字符串计算 MenuItem 的宽度,默认为 NO. */
  146. @property (nonatomic, assign) BOOL automaticallyCalculatesItemWidths;
  147. /** Whether the controller can scroll. Default is YES. */
  148. @property (nonatomic, assign) BOOL scrollEnable;
  149. /**
  150. * 选中时的标题尺寸
  151. * The title size when selected (animatable)
  152. */
  153. @property (nonatomic, assign) CGFloat titleSizeSelected;
  154. /**
  155. * 非选中时的标题尺寸
  156. * The normal title size (animatable)
  157. */
  158. @property (nonatomic, assign) CGFloat titleSizeNormal;
  159. /**
  160. * 标题选中时的颜色, 颜色是可动画的.
  161. * The title color when selected, the color is animatable.
  162. */
  163. @property (nonatomic, strong) UIColor *titleColorSelected;
  164. /**
  165. * 标题非选择时的颜色, 颜色是可动画的.
  166. * The title's normal color, the color is animatable.
  167. */
  168. @property (nonatomic, strong) UIColor *titleColorNormal;
  169. /**
  170. * 标题的字体名字
  171. * The name of title's font
  172. */
  173. @property (nonatomic, nullable, copy) NSString *titleFontName;
  174. /**
  175. * 每个 MenuItem 的宽度
  176. * The item width,when all are same,use this property
  177. */
  178. @property (nonatomic, assign) CGFloat menuItemWidth;
  179. /**
  180. * 各个 MenuItem 的宽度,可不等,数组内为 NSNumber.
  181. * Each item's width, when they are not all the same, use this property, Put `NSNumber` in this array.
  182. */
  183. @property (nonatomic, nullable, copy) NSArray<NSNumber *> *itemsWidths;
  184. /**
  185. * Menu view 的样式,默认为无下划线
  186. * Menu view's style, now has two different styles, 'Line','default'
  187. */
  188. @property (nonatomic, assign) WMMenuViewStyle menuViewStyle;
  189. @property (nonatomic, assign) WMMenuViewLayoutMode menuViewLayoutMode;
  190. /**
  191. * 进度条的颜色,默认和选中颜色一致(如果 style 为 Default,则该属性无用)
  192. * The progress's color,the default color is same with `titleColorSelected`.If you want to have a different color, set this property.
  193. */
  194. @property (nonatomic, nullable, strong) UIColor *progressColor;
  195. /**
  196. * 定制进度条在各个 item 下的宽度
  197. */
  198. @property (nonatomic, nullable, strong) NSArray *progressViewWidths;
  199. /// 定制进度条,若每个进度条长度相同,可设置该属性
  200. @property (nonatomic, assign) CGFloat progressWidth;
  201. /// 调皮效果,用于实现腾讯视频新效果,请设置一个较小的 progressWidth
  202. @property (nonatomic, assign) BOOL progressViewIsNaughty;
  203. /**
  204. * 是否发送在创建控制器或者视图完全展现在用户眼前时通知观察者,默认为不开启,如需利用通知请开启
  205. * Whether notify observer when finish init or fully displayed to user, the default is NO.
  206. * See `WMPageConst.h` for more information.
  207. */
  208. @property (nonatomic, assign) BOOL postNotification;
  209. /**
  210. * 是否记录 Controller 的位置,并在下次回来的时候回到相应位置,默认为 NO (若当前缓存中存在不会触发)
  211. * Whether to remember controller's positon if it's a kind of scrollView controller,like UITableViewController,The default value is NO.
  212. * 比如 `UITabelViewController`, 当然你也可以在自己的控制器中自行设置, 如果将 Controller.view 替换为 scrollView 或者在Controller.view 上添加了一个和自身 bounds 一样的 scrollView 也是OK的
  213. */
  214. @property (nonatomic, assign) BOOL rememberLocation __deprecated_msg("Because of the cache policy,this property can abondon now.");
  215. /** 缓存的机制,默认为无限制 (如果收到内存警告, 会自动切换) */
  216. @property (nonatomic, assign) WMPageControllerCachePolicy cachePolicy;
  217. /** 预加载机制,在停止滑动的时候预加载 n 页 */
  218. @property (nonatomic, assign) WMPageControllerPreloadPolicy preloadPolicy;
  219. /** Whether ContentView bounces */
  220. @property (nonatomic, assign) BOOL bounces;
  221. /**
  222. * 是否作为 NavigationBar 的 titleView 展示,默认 NO
  223. * Whether to show on navigation bar, the default value is `NO`
  224. */
  225. @property (assign, nonatomic) BOOL showOnNavigationBar;
  226. /**
  227. * 用代码设置 contentView 的 contentOffset 之前,请设置 startDragging = YES
  228. * Set startDragging = YES before set contentView.contentOffset = xxx;
  229. */
  230. @property (nonatomic, assign) BOOL startDragging;
  231. /** 下划线进度条的高度 */
  232. @property (nonatomic, assign) CGFloat progressHeight;
  233. /**
  234. * Menu view items' margin / make sure it's count is equal to (controllers' count + 1),default is 0
  235. 顶部菜单栏各个 item 的间隙,因为包括头尾两端,所以确保它的数量等于控制器数量 + 1, 默认间隙为 0
  236. */
  237. @property (nonatomic, nullable, copy) NSArray<NSNumber *> *itemsMargins;
  238. /**
  239. * set itemMargin if all margins are the same, default is 0
  240. 如果各个间隙都想同,设置该属性,默认为 0
  241. */
  242. @property (nonatomic, assign) CGFloat itemMargin;
  243. /** progressView 到 menuView 底部的距离 */
  244. @property (nonatomic, assign) CGFloat progressViewBottomSpace;
  245. /** progressView's cornerRadius */
  246. @property (nonatomic, assign) CGFloat progressViewCornerRadius;
  247. /** 顶部导航栏 */
  248. @property (nonatomic, nullable, weak) WMMenuView *menuView;
  249. /** 内部容器 */
  250. @property (nonatomic, nullable, weak) WMScrollView *scrollView;
  251. /** MenuView 内部视图与左右的间距 */
  252. @property (nonatomic, assign) CGFloat menuViewContentMargin;
  253. /**
  254. * 构造方法,请使用该方法创建控制器. 或者实现数据源方法. /
  255. * Init method,recommend to use this instead of `-init`. Or you can implement datasource by yourself.
  256. *
  257. * @param classes 子控制器的 class,确保数量与 titles 的数量相等
  258. * @param titles 各个子控制器的标题,用 NSString 描述
  259. *
  260. * @return instancetype
  261. */
  262. - (instancetype)initWithViewControllerClasses:(NSArray<Class> *)classes andTheirTitles:(NSArray<NSString *> *)titles;
  263. /**
  264. * A method in order to reload MenuView and child view controllers. If you had set `itemsMargins` or `itemsWidths` `values` and `keys` before, make sure you have update them also before you call this method. And most important, PAY ATTENTION TO THE COUNT OF THOSE ARRAY.
  265. 该方法用于重置刷新父控制器,该刷新包括顶部 MenuView 和 childViewControllers.如果之前设置过 `itemsMargins` 和 `itemsWidths` `values` 以及 `keys` 属性,请确保在调用 reload 之前也同时更新了这些属性。并且,最最最重要的,注意数组的个数以防止溢出。
  266. */
  267. - (void)reloadData;
  268. /**
  269. Layout all views in WMPageController
  270. @discussion This method will recall `-pageController:preferredFrameForContentView:` and `-pageContoller:preferredFrameForMenuView:`
  271. */
  272. - (void)forceLayoutSubviews;
  273. /**
  274. * Update designated item's title
  275. 更新指定序号的控制器的标题
  276. *
  277. * @param title 新的标题
  278. * @param index 目标序号
  279. */
  280. - (void)updateTitle:(NSString *)title atIndex:(NSInteger)index;
  281. /**
  282. * Update designated item's title and width
  283. 更新指定序号的控制器的标题以及他的宽度
  284. *
  285. * @param title 新的标题
  286. * @param index 目标序号
  287. * @param width 对应item的新宽度
  288. */
  289. - (void)updateTitle:(NSString *)title andWidth:(CGFloat)width atIndex:(NSInteger)index;
  290. - (void)updateAttributeTitle:(NSAttributedString *)title atIndex:(NSInteger)index;
  291. /** 当 app 即将进入后台接收到的通知 */
  292. - (void)willResignActive:(NSNotification *)notification;
  293. /** 当 app 即将回到前台接收到的通知 */
  294. - (void)willEnterForeground:(NSNotification *)notification;
  295. @end
  296. NS_ASSUME_NONNULL_END