SwitchClientPresentationController.m 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // SwitchClientPresentationController.m
  3. // model
  4. //
  5. // Created by Drew on 2018/12/14.
  6. // Copyright © 2018 Mine. All rights reserved.
  7. //
  8. #import "SwitchClientPresentationController.h"
  9. @implementation SwitchClientPresentationController
  10. - (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController {
  11. self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController];
  12. if (self) {
  13. // 必须设置 presentedViewController 的 modalPresentationStyle
  14. // 在自定义动画效果的情况下,苹果强烈建议设置为 UIModalPresentationCustom
  15. presentedViewController.modalPresentationStyle = UIModalPresentationCustom;
  16. }
  17. return self;
  18. }
  19. // 呈现过渡即将开始的时候被调用的
  20. // 可以在此方法创建和设置自定义动画所需的view
  21. - (void)presentationTransitionWillBegin {
  22. self.containerView.layer.cornerRadius = 14;
  23. // 背景遮罩
  24. UIView *dimmingView = [[UIView alloc] initWithFrame:self.containerView.bounds];
  25. dimmingView.backgroundColor = [UIColor blackColor];
  26. dimmingView.opaque = NO; //是否透明
  27. dimmingView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  28. [dimmingView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dimmingViewTapped:)]];
  29. self.dimmingView = dimmingView;
  30. [self.containerView addSubview:dimmingView]; // 添加到动画容器View中。
  31. // 获取presentingViewController 的转换协调器,应该动画期间的一个类?上下文?之类的,负责动画的一个东西
  32. id<UIViewControllerTransitionCoordinator> transitionCoordinator = self.presentingViewController.transitionCoordinator;
  33. // 动画期间,背景View的动画方式
  34. self.dimmingView.alpha = 0.f;
  35. [transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  36. self.dimmingView.alpha = 0.4f;
  37. } completion:NULL];
  38. }
  39. #pragma mark 点击了背景遮罩view
  40. - (void)dimmingViewTapped:(UITapGestureRecognizer*)sender {
  41. [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
  42. }
  43. // 在呈现过渡结束时被调用的,并且该方法提供一个布尔变量来判断过渡效果是否完成
  44. - (void)presentationTransitionDidEnd:(BOOL)completed {
  45. // 在取消动画的情况下,可能为NO,这种情况下,应该取消视图的引用,防止视图没有释放
  46. if (!completed) {
  47. self.dimmingView = nil;
  48. }
  49. }
  50. // 消失过渡即将开始的时候被调用的
  51. - (void)dismissalTransitionWillBegin {
  52. id<UIViewControllerTransitionCoordinator> transitionCoordinator = self.presentingViewController.transitionCoordinator;
  53. [transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  54. self.dimmingView.alpha = 0.f;
  55. } completion:NULL];
  56. }
  57. // 消失过渡完成之后调用,此时应该将视图移除,防止强引用
  58. - (void)dismissalTransitionDidEnd:(BOOL)completed {
  59. if (completed == YES) {
  60. [self.dimmingView removeFromSuperview];
  61. self.dimmingView = nil;
  62. }
  63. }
  64. // 返回目标控制器Viewframe
  65. - (CGRect)frameOfPresentedViewInContainerView {
  66. // 这里直接按照想要的大小写死,其实这样写不好,在第二个Demo里,我们将按照苹果官方Demo,写灵活的获取方式。
  67. CGFloat height = 260.f;
  68. CGFloat width = 300.f;
  69. CGRect containerViewBounds = CGRectMake((self.containerView.bounds.size.width - width) / 2, (self.containerView.bounds.size.height - height) / 2, width, height);
  70. return containerViewBounds;
  71. }
  72. // 建议就这样重写就行,这个应该是控制器内容大小变化时,就会调用这个方法, 比如适配横竖屏幕时,翻转屏幕时
  73. // 可以使用UIContentContainer的方法来调整任何子视图控制器的大小或位置。
  74. - (void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container {
  75. [super preferredContentSizeDidChangeForChildContentContainer:container];
  76. if (container == self.presentedViewController) [self.containerView setNeedsLayout];
  77. }
  78. - (void)containerViewWillLayoutSubviews {
  79. [super containerViewWillLayoutSubviews];
  80. self.dimmingView.frame = self.containerView.bounds;
  81. }
  82. @end