UIViewController+PanModalPresenter.m 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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:(nullable UIView *)sourceView sourceRect:(CGRect)rect {
  15. HWPanModalPresentationDelegate *delegate = [HWPanModalPresentationDelegate new];
  16. viewControllerToPresent.presentationDelegate = 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. viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
  31. [self presentViewController:viewControllerToPresent animated:YES completion:nil];
  32. });
  33. }
  34. - (void)presentPanModal:(UIViewController <HWPanModalPresentable> *)viewControllerToPresent {
  35. [self presentPanModal:viewControllerToPresent sourceView:nil sourceRect:CGRectZero];
  36. }
  37. - (HWPanModalPresentationDelegate *)presentationDelegate {
  38. return objc_getAssociatedObject(self, _cmd);
  39. }
  40. - (void)setPresentationDelegate:(HWPanModalPresentationDelegate *)presentationDelegate {
  41. objc_setAssociatedObject(self, @selector(presentationDelegate), presentationDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  42. }
  43. @end