HWPanModalPresentable.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. //
  2. // HWPanModalPresentable.h
  3. // Pods
  4. //
  5. // Created by heath wang on 2019/4/26.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import <UIKit/UIKit.h>
  9. #import <HWPanModal/HWPanModalHeight.h>
  10. #import <HWPanModal/HWPresentingVCAnimatedTransitioning.h>
  11. #import <HWPanModal/HWPanModalIndicatorProtocol.h>
  12. #import <HWPanModal/HWBackgroundConfig.h>
  13. NS_ASSUME_NONNULL_BEGIN
  14. typedef NS_ENUM(NSInteger, PresentationState) {
  15. PresentationStateShort NS_SWIFT_NAME(short),
  16. PresentationStateMedium NS_SWIFT_NAME(medium),
  17. PresentationStateLong NS_SWIFT_NAME(long),
  18. };
  19. typedef NS_ENUM(NSInteger, PresentingViewControllerAnimationStyle) {
  20. // no animation for presentingVC
  21. PresentingViewControllerAnimationStyleNone NS_SWIFT_NAME(none),
  22. // page sheet animation, like iOS13 default modalPresentation style
  23. PresentingViewControllerAnimationStylePageSheet NS_SWIFT_NAME(pageSheet),
  24. // shopping cart animation, like jd/taobao shopping cart animation
  25. PresentingViewControllerAnimationStyleShoppingCart NS_SWIFT_NAME(shoppingCart),
  26. // make your own custom animation
  27. PresentingViewControllerAnimationStyleCustom NS_SWIFT_NAME(custom),
  28. };
  29. /**
  30. * HWPanModalPresentable为present配置协议
  31. * 默认情况下无需实现,只需Controller/View适配该协议
  32. * 通过category来默认实现以下所有方法,避免继承类
  33. *
  34. * This Protocol is the core of HWPanModal, we use it to config presentation.
  35. * Default, you don't need to conform all of these methods, just implement what you want to customize.
  36. * All the config has default value, we use a `UIViewController` category to conform `HWPanModalPresentable` protocol.
  37. */
  38. @protocol HWPanModalPresentable <NSObject>
  39. #pragma mark - ScrollView Config
  40. /**
  41. * 支持同步拖拽的scrollView
  42. * 如果ViewController中包含scrollView并且你想scrollView滑动和拖拽手势同时存在,请返回此scrollView
  43. *
  44. * If your ViewController has a scrollable view(UIScrollView and subclass), and you want pan gesture and scrollable both work, return it.
  45. */
  46. - (nullable UIScrollView *)panScrollable;
  47. /**
  48. * determine ScrollView scrollEnabled
  49. * default is YES
  50. */
  51. - (BOOL)isPanScrollEnabled;
  52. /**
  53. * scrollView指示器insets
  54. * Use `panModalSetNeedsLayoutUpdate()` when updating insets.
  55. */
  56. - (UIEdgeInsets)scrollIndicatorInsets;
  57. /**
  58. * A Boolean value that controls whether the scrollable vertical scroll indicator is visible.
  59. * default is YES.
  60. */
  61. - (BOOL)showsScrollableVerticalScrollIndicator;
  62. /**
  63. * default is YES.
  64. */
  65. - (BOOL)shouldAutoSetPanScrollContentInset;
  66. /**
  67. * 是否允许拖动额外拖动,如果panScrollable存在,且scrollView contentSize > (size + bottomLayoutOffset),返回YES
  68. * 其余情况返回NO
  69. *
  70. * If panScrollable exists, and scrollView contentSize > (size + bottomLayoutOffset), auto return YES, otherwise return NO.
  71. * You can make your own logic if you want, and you know what you are doing.
  72. */
  73. - (BOOL)allowsExtendedPanScrolling;
  74. #pragma mark - Offset/position
  75. /**
  76. * Screen top offset from presented viewController
  77. * Default is topLayoutGuide.length + 21.0.
  78. */
  79. - (CGFloat)topOffset;
  80. /**
  81. * 当pan状态为short时候的高度
  82. * default: shortFormHeight = longFormHeight
  83. */
  84. - (PanModalHeight)shortFormHeight;
  85. /**
  86. * default: mediumFormHeight = longFormHeight
  87. */
  88. - (PanModalHeight)mediumFormHeight;
  89. /**
  90. * 当pan状态为long的高度
  91. */
  92. - (PanModalHeight)longFormHeight;
  93. /**
  94. * 初始弹出高度状态,默认为`shortFormHeight`
  95. *
  96. * Origin presentation height state, if you have special requirement, change it.
  97. * Default is `shortFormHeight`
  98. */
  99. - (PresentationState)originPresentationState;
  100. #pragma mark - Animation config
  101. /**
  102. * spring弹性动画数值
  103. * Default is 0.9
  104. */
  105. - (CGFloat)springDamping;
  106. /**
  107. * 转场动画时间
  108. * Default is 0.5 second
  109. */
  110. - (NSTimeInterval)transitionDuration;
  111. /**
  112. * starting from version 0.6.5, Only works when dismiss
  113. * Default is same as `- (NSTimeInterval)transitionDuration;`
  114. */
  115. - (NSTimeInterval)dismissalDuration;
  116. /**
  117. * 转场动画options
  118. * Default is UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState
  119. */
  120. - (UIViewAnimationOptions)transitionAnimationOptions;
  121. #pragma mark - AppearanceTransition
  122. /**
  123. * If enabled, the presenting VC will invoke viewWillAppear:, viewWillDisappear:
  124. * Default is YES
  125. */
  126. - (BOOL)shouldEnableAppearanceTransition;
  127. #pragma mark - Background config
  128. /**
  129. * use this object to config background alpha or blur effect
  130. * @return background config object
  131. */
  132. - (HWBackgroundConfig *)backgroundConfig;
  133. #pragma mark - User Interaction
  134. /**
  135. * 该bool值控制当pan View状态为long的情况下,是否可以继续拖拽到PanModalHeight = MAX的情况
  136. * 默认为YES,即当已经拖拽到long的情况下不能再继续拖动
  137. */
  138. - (BOOL)anchorModalToLongForm;
  139. /**
  140. * 是否允许点击背景处dismiss presented Controller
  141. * 默认为YES
  142. */
  143. - (BOOL)allowsTapBackgroundToDismiss;
  144. /**
  145. * 是否允许drag操作dismiss presented Controller
  146. * Default is YES
  147. */
  148. - (BOOL)allowsDragToDismiss;
  149. /// Default is YES, When return NO, and you did set shortForm, user CAN NOT pull down the view.
  150. - (BOOL)allowsPullDownWhenShortState;
  151. /**
  152. min Velocity from Vertical direction that trigger dismiss action.
  153. Default is 300.0
  154. */
  155. - (CGFloat)minVerticalVelocityToTriggerDismiss;
  156. /**
  157. * 是否允许用户操作
  158. * Default is YES
  159. */
  160. - (BOOL)isUserInteractionEnabled;
  161. /**
  162. * 是否允许触觉反馈
  163. * Default is YES
  164. */
  165. - (BOOL)isHapticFeedbackEnabled;
  166. /**
  167. * 是否允许触摸事件透传到presenting ViewController/View。如果你有特殊需求的话(比如弹出一个底部视图,但是你想操作弹出视图下面的view,即presenting VC/View),可开启此功能
  168. *
  169. * Whether allows touch events passing through the transition container view.
  170. * In some situations, you present the bottom VC/View, and you want to operate the presenting VC/View(mapView, scrollView and etc), enable this func.
  171. *
  172. * Note: You SHOULD MUST dismiss the presented VC in the right time.
  173. */
  174. - (BOOL)allowsTouchEventsPassingThroughTransitionView;
  175. #pragma mark - Screen left egde interaction
  176. /**
  177. * 是否允许屏幕边缘侧滑手势
  178. * Default is NO,not allowed this user interaction.
  179. *
  180. * Note: Currently only works on UIViewController.
  181. */
  182. - (BOOL)allowScreenEdgeInteractive;
  183. /**
  184. * Max allowed distance to screen left edge when you want to make screen edge pan interaction
  185. * Default is 0, means it will ignore this limit, full screen left edge pan will work.
  186. * @return distance to left screen edge
  187. */
  188. - (CGFloat)maxAllowedDistanceToLeftScreenEdgeForPanInteraction;
  189. /**
  190. * When you enabled `- (BOOL)allowScreenEdgeInteractive`, this can work.
  191. * min horizontal velocity to trigger screen edge dismiss if the drag didn't reach 0.5 screen width.
  192. * Default is 500
  193. */
  194. - (CGFloat)minHorizontalVelocityToTriggerScreenEdgeDismiss;
  195. #pragma mark - Customize presentingViewController animation
  196. /**
  197. * Config presentingViewController animation style, this animations will work for present & dismiss.
  198. * Default is `PresentingViewControllerAnimationStyleNone`.
  199. * @return The animation style.
  200. */
  201. - (PresentingViewControllerAnimationStyle)presentingVCAnimationStyle;
  202. /**
  203. * 自定义presenting ViewController转场动画,默认为nil
  204. * 注意:如果实现该方法并返回非空示例,要使该方法生效,`- (PresentingViewControllerAnimationStyle)presentingVCAnimationStyle`必须返回PresentingViewControllerAnimationStyleCustom
  205. *
  206. * custom presenting ViewController transition animation, default is nil
  207. * Note: If you implement this method and return non nil value, You must implement `- (PresentingViewControllerAnimationStyle)
  208. * presentingVCAnimationStyle` and return PresentingViewControllerAnimationStyleCustom
  209. */
  210. - (nullable id<HWPresentingViewControllerAnimatedTransitioning>)customPresentingVCAnimation;
  211. #pragma mark - Content UI config
  212. /**
  213. * 是否顶部圆角
  214. * Default is YES
  215. */
  216. - (BOOL)shouldRoundTopCorners;
  217. /**
  218. * 顶部圆角数值
  219. * Default is 8.0
  220. */
  221. - (CGFloat)cornerRadius;
  222. /**
  223. * presented content shadow
  224. * Default is None config
  225. */
  226. - (HWPanModalShadow)contentShadow;
  227. #pragma mark - Indicator config
  228. /**
  229. * 是否显示drag指示view
  230. * Default is YES,Default this method depend on `- (BOOL)shouldRoundTopCorners`
  231. */
  232. - (BOOL)showDragIndicator;
  233. /**
  234. * You can make the indicator customized. Just adopt `HWPanModalIndicatorProtocol`
  235. * Default this method return nil, Then the default indicator will be used.
  236. */
  237. - (__kindof UIView<HWPanModalIndicatorProtocol> * _Nullable)customIndicatorView;
  238. #pragma mark - Keyboard handle
  239. /**
  240. * When there is text input view exists and becomeFirstResponder, will auto handle keyboard height.
  241. * Default is YES. You can disable it, handle it by yourself.
  242. */
  243. - (BOOL)isAutoHandleKeyboardEnabled;
  244. /**
  245. The offset that keyboard show from input view's bottom. It works when
  246. `isAutoHandleKeyboardEnabled` return YES.
  247. @return offset, default is 5.
  248. */
  249. - (CGFloat)keyboardOffsetFromInputView;
  250. #pragma mark - Delegate
  251. #pragma mark - Pan Gesture delegate
  252. /**
  253. * 询问delegate是否需要使拖拽手势生效
  254. * 若返回NO,则禁用拖拽手势操作,即不能拖拽dismiss
  255. * 默认为YES
  256. */
  257. - (BOOL)shouldRespondToPanModalGestureRecognizer:(nonnull UIPanGestureRecognizer *)panGestureRecognizer;
  258. /**
  259. * 当pan recognizer状态为begin/changed时,通知delegate回调。
  260. * 当拖动presented View时,该方法会持续的回调
  261. * 默认实现为空
  262. */
  263. - (void)willRespondToPanModalGestureRecognizer:(nonnull UIPanGestureRecognizer *)panGestureRecognizer;
  264. /**
  265. * 内部处理完成拖动操作后触发此回调,此时view frame可能已经变化。
  266. * Framework has did finish logic for GestureRecognizer delegate. It will call many times when you darg.
  267. */
  268. - (void)didRespondToPanModalGestureRecognizer:(nonnull UIPanGestureRecognizer *)panGestureRecognizer;
  269. /**
  270. * 内部处理完成拖动操作后触发此回调,此时view frame可能已经变化。
  271. * Framework has did finish logic for GestureRecognizer delegate. It will call many times when you darg.
  272. */
  273. - (void)didEndRespondToPanModalGestureRecognizer:(nonnull UIPanGestureRecognizer *)panGestureRecognizer;
  274. /**
  275. * 是否优先执行dismiss拖拽手势,当存在panScrollable的情况下,如果此方法返回YES,则
  276. * dismiss手势生效,scrollView本身的滑动则不再生效。也就是说可以拖动Controller view,而scrollView没法拖动了。
  277. *
  278. * 例子:controller view上添加一个TableView,并铺满全屏,然后在controller view 顶部添加一个一定大小的viewA,
  279. * 这个时候会发现viewA有时候无法拖动,可以实现此delegate方法来解决
  280. ```
  281. - (BOOL)shouldPrioritizePanModalGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer {
  282. CGPoint loc = [panGestureRecognizer locationInView:self.view];
  283. // check whether user pan action in viewA
  284. if (CGRectContainsPoint(self.viewA.frame, loc)) {
  285. return YES;
  286. }
  287. return NO;
  288. }
  289. ```
  290. * 默认为NO
  291. *
  292. * This delegate is useful when you want panGestureRecognizer has a high prioritize and
  293. * make scrollable does NOT scroll.
  294. * Example: You controller add a full size tableView, then add viewA on top of your controller view.
  295. * Now you find you can not drag the viewA, use this delegate to resolve problem.
  296. * Please refer to code above this comment.
  297. *
  298. * Default is NO
  299. */
  300. - (BOOL)shouldPrioritizePanModalGestureRecognizer:(nonnull UIPanGestureRecognizer *)panGestureRecognizer;
  301. /**
  302. * When you pan present controller to dismiss, and the view's y <= shortFormYPos,
  303. * this delegate method will be called.
  304. * @param percent 0 ~ 1, 1 means has dismissed
  305. */
  306. - (void)panModalGestureRecognizer:(nonnull UIPanGestureRecognizer *)panGestureRecognizer dismissPercent:(CGFloat)percent;
  307. #pragma mark - PresentationState change delegate
  308. /**
  309. * 是否应该变更panModal状态
  310. */
  311. - (BOOL)shouldTransitionToState:(PresentationState)state;
  312. /**
  313. * called when the Transition State will change.
  314. * 通知回调即将变更状态
  315. */
  316. - (void)willTransitionToState:(PresentationState)state;
  317. /**
  318. * PresentationState did change callback
  319. */
  320. - (void)didChangeTransitionToState:(PresentationState)state;
  321. #pragma mark - present delegate
  322. /**
  323. * call when present transition will begin.
  324. */
  325. - (void)panModalTransitionWillBegin;
  326. /**
  327. * call when present transition did finish.
  328. */
  329. - (void)panModalTransitionDidFinish;
  330. /**
  331. * call when your custom presented vc has been added to the presentation container.
  332. */
  333. - (void)presentedViewDidMoveToSuperView;
  334. #pragma mark - Dismiss delegate
  335. /**
  336. * will dismiss
  337. */
  338. - (void)panModalWillDismiss;
  339. /**
  340. * Did finish dismissing
  341. */
  342. - (void)panModalDidDismissed;
  343. #pragma mark - DEPRECATED DECLARE
  344. /**
  345. * 是否对presentingViewController做动画效果,默认该效果类似淘宝/京东购物车凹陷效果
  346. * 默认为NO
  347. */
  348. - (BOOL)shouldAnimatePresentingVC DEPRECATED_MSG_ATTRIBUTE("This api has been marked as DEPRECATED on version 0.3.6, please use `- (PresentingViewControllerAnimationStyle)presentingVCAnimationStyle` replaced.");
  349. /**
  350. * 背景透明度
  351. * Default is 0.7
  352. */
  353. - (CGFloat)backgroundAlpha DEPRECATED_MSG_ATTRIBUTE("This api has been marked as DEPRECATED on version 0.7.0, please use `- (HWBackgroundConfig *)backgroundConfig` replaced.");
  354. /**
  355. * Blur background
  356. * This function can NOT coexist with backgroundAlpha
  357. * Default use backgroundAlpha, Once you set backgroundBlurRadius > 0, blur will work.
  358. * Recommend set the value 10 ~ 20.
  359. * @return blur radius
  360. */
  361. - (CGFloat)backgroundBlurRadius DEPRECATED_MSG_ATTRIBUTE("This api has been marked as DEPRECATED on version 0.7.0, please use `- (HWBackgroundConfig *)backgroundConfig` replaced.");
  362. /**
  363. * blur background color
  364. * @return color, default is White Color.
  365. */
  366. - (nonnull UIColor *)backgroundBlurColor DEPRECATED_MSG_ATTRIBUTE("This api has been marked as DEPRECATED on version 0.7.0, please use `- (HWBackgroundConfig *)backgroundConfig` replaced.");
  367. @end
  368. NS_ASSUME_NONNULL_END