JLPresentationController.m 3.2 KB

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