RAPresentationController.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // RApresentationController.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/8/28.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAPresentationController.h"
  9. @interface RAPresentationController ()
  10. @property (nonatomic,strong) UIView *dimmingView;
  11. @end
  12. @implementation RAPresentationController
  13. - (UIView *)dimmingView {
  14. if (!_dimmingView) {
  15. _dimmingView = [UIView new];
  16. _dimmingView.backgroundColor = [UIColor grayColor];
  17. }
  18. return _dimmingView;
  19. }
  20. - (void)presentationTransitionWillBegin {
  21. self.dimmingView.frame = self.containerView.bounds;
  22. self.dimmingView.alpha = 0.f;
  23. [self.containerView addSubview:self.dimmingView];
  24. [self.containerView addSubview:self.presentedView];
  25. // 确保我们的动画与其他动画一道儿播放。
  26. id<UIViewControllerTransitionCoordinator> transitionCoordiantor = self.presentedViewController.transitionCoordinator;
  27. [transitionCoordiantor animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  28. self.dimmingView.alpha = .4f;
  29. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  30. }];
  31. }
  32. - (void)presentationTransitionDidEnd:(BOOL)completed {
  33. if (completed) {
  34. // [self.dimmingView removeFromSuperview];
  35. }
  36. }
  37. - (CGRect)frameOfPresentedViewInContainerView {
  38. CGSize size = self.containerView.bounds.size;
  39. UIViewController *presentedVC = self.presentedViewController;
  40. size = presentedVC.preferredContentSize;
  41. if (size.width <= 0 || size.height <= 0) {
  42. size = self.containerView.bounds.size;
  43. }
  44. CGFloat w = CGRectGetWidth(self.containerView.bounds);
  45. CGFloat h = CGRectGetHeight(self.containerView.bounds);
  46. return CGRectMake((w - size.width) * 0.5, (h - size.height) * 0.5, size.width, size.height);
  47. }
  48. - (void)dismissalTransitionWillBegin {
  49. // 确保我们的动画与其他动画一道儿播放。
  50. id<UIViewControllerTransitionCoordinator> transitionCoordiantor = self.presentedViewController.transitionCoordinator;
  51. [transitionCoordiantor animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  52. self.dimmingView.alpha = 0.f;
  53. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  54. }];
  55. }
  56. - (void)dismissalTransitionDidEnd:(BOOL)completed {
  57. if (completed) {
  58. // [self.dimmingView removeFromSuperview];
  59. }
  60. }
  61. // 屏幕旋转调用此方法
  62. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  63. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  64. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  65. self.dimmingView.frame = self.containerView.bounds;
  66. self.presentedView.frame = [self frameOfPresentedViewInContainerView];
  67. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  68. }];
  69. }
  70. @end