UIViewController+PanModalPresenter.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // UIViewController+PanModalPresenter.m
  3. // HWPanModal
  4. //
  5. // Created by heath wang on 2019/4/29.
  6. //
  7. #import "UIViewController+PanModalPresenter.h"
  8. #import "HWPanModalPresentationDelegate.h"
  9. #import <objc/runtime.h>
  10. @implementation UIViewController (PanModalPresenter)
  11. - (BOOL)isPanModalPresented {
  12. return [self.transitioningDelegate isKindOfClass:HWPanModalPresentationDelegate.class];
  13. }
  14. - (void)presentPanModal:(UIViewController<HWPanModalPresentable> *)viewControllerToPresent sourceView:(UIView *)sourceView sourceRect:(CGRect)rect completion:(void (^)(void))completion {
  15. HWPanModalPresentationDelegate *delegate = [HWPanModalPresentationDelegate new];
  16. viewControllerToPresent.hw_panModalPresentationDelegate = delegate;
  17. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad &&
  18. (sourceView && !CGRectEqualToRect(rect, CGRectZero))) {
  19. viewControllerToPresent.modalPresentationStyle = UIModalPresentationPopover;
  20. viewControllerToPresent.popoverPresentationController.sourceRect = rect;
  21. viewControllerToPresent.popoverPresentationController.sourceView = sourceView;
  22. viewControllerToPresent.popoverPresentationController.delegate = delegate;
  23. } else {
  24. viewControllerToPresent.modalPresentationStyle = UIModalPresentationCustom;
  25. viewControllerToPresent.modalPresentationCapturesStatusBarAppearance = YES;
  26. viewControllerToPresent.transitioningDelegate = delegate;
  27. }
  28. // fix for iOS 8 issue: the present action will delay.
  29. dispatch_async(dispatch_get_main_queue(), ^{
  30. [self presentViewController:viewControllerToPresent animated:YES completion:completion];
  31. });
  32. }
  33. - (void)presentPanModal:(UIViewController <HWPanModalPresentable> *)viewControllerToPresent sourceView:(nullable UIView *)sourceView sourceRect:(CGRect)rect {
  34. [self presentPanModal:viewControllerToPresent sourceView:sourceView sourceRect:rect completion:nil];
  35. }
  36. - (void)presentPanModal:(UIViewController <HWPanModalPresentable> *)viewControllerToPresent {
  37. [self presentPanModal:viewControllerToPresent sourceView:nil sourceRect:CGRectZero];
  38. }
  39. - (void)presentPanModal:(UIViewController<HWPanModalPresentable> *)viewControllerToPresent completion:(void (^)(void))completion {
  40. [self presentPanModal:viewControllerToPresent sourceView:nil sourceRect:CGRectZero completion:completion];
  41. }
  42. - (HWPanModalPresentationDelegate *)hw_panModalPresentationDelegate {
  43. return objc_getAssociatedObject(self, _cmd);
  44. }
  45. - (void)setHw_panModalPresentationDelegate:(HWPanModalPresentationDelegate *)hw_panModalPresentationDelegate {
  46. objc_setAssociatedObject(self, @selector(hw_panModalPresentationDelegate), hw_panModalPresentationDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  47. }
  48. @end