AdViewController.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // AdViewController.m
  3. // 千模
  4. //
  5. // Created by Drew on 2019/1/4.
  6. // Copyright © 2019 MUMEI. All rights reserved.
  7. //
  8. #import "AdViewController.h"
  9. #import "Masonry.h"
  10. #import "WebViewController.h"
  11. @interface AdPresenter : UIPresentationController
  12. @property(nonatomic, strong) UIView *dimmingView;
  13. @end
  14. @implementation AdPresenter : UIPresentationController
  15. - (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController {
  16. self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController];
  17. if (self) {
  18. // 必须设置 presentedViewController 的 modalPresentationStyle
  19. // 在自定义动画效果的情况下,苹果强烈建议设置为 UIModalPresentationCustom
  20. presentedViewController.modalPresentationStyle = UIModalPresentationCustom;
  21. }
  22. return self;
  23. }
  24. // 呈现过渡即将开始的时候被调用的
  25. // 可以在此方法创建和设置自定义动画所需的view
  26. - (void)presentationTransitionWillBegin {
  27. self.containerView.layer.cornerRadius = 14;
  28. // 背景遮罩
  29. UIView *dimmingView = [[UIView alloc] initWithFrame:self.containerView.bounds];
  30. dimmingView.backgroundColor = [UIColor blackColor];
  31. dimmingView.opaque = NO; //是否透明
  32. dimmingView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  33. [dimmingView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dimmingViewTapped:)]];
  34. self.dimmingView = dimmingView;
  35. [self.containerView addSubview:dimmingView]; // 添加到动画容器View中。
  36. // 获取presentingViewController 的转换协调器,应该动画期间的一个类?上下文?之类的,负责动画的一个东西
  37. id <UIViewControllerTransitionCoordinator> transitionCoordinator = self.presentingViewController.transitionCoordinator;
  38. // 动画期间,背景View的动画方式
  39. self.dimmingView.alpha = 0.f;
  40. [transitionCoordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context) {
  41. self.dimmingView.alpha = 0.4f;
  42. } completion:NULL];
  43. }
  44. #pragma mark 点击了背景遮罩view
  45. - (void)dimmingViewTapped:(UITapGestureRecognizer *)sender {
  46. [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
  47. }
  48. // 在呈现过渡结束时被调用的,并且该方法提供一个布尔变量来判断过渡效果是否完成
  49. - (void)presentationTransitionDidEnd:(BOOL)completed {
  50. // 在取消动画的情况下,可能为NO,这种情况下,应该取消视图的引用,防止视图没有释放
  51. if (!completed) {
  52. self.dimmingView = nil;
  53. }
  54. }
  55. // 消失过渡即将开始的时候被调用的
  56. - (void)dismissalTransitionWillBegin {
  57. id <UIViewControllerTransitionCoordinator> transitionCoordinator = self.presentingViewController.transitionCoordinator;
  58. [transitionCoordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context) {
  59. self.dimmingView.alpha = 0.f;
  60. } completion:NULL];
  61. }
  62. // 消失过渡完成之后调用,此时应该将视图移除,防止强引用
  63. - (void)dismissalTransitionDidEnd:(BOOL)completed {
  64. if (completed == YES) {
  65. [self.dimmingView removeFromSuperview];
  66. self.dimmingView = nil;
  67. }
  68. }
  69. // 返回目标控制器Viewframe
  70. - (CGRect)frameOfPresentedViewInContainerView {
  71. CGFloat width = ScreenWidth - 96;
  72. CGFloat height = 386.f;
  73. CGRect containerViewBounds = CGRectMake((self.containerView.bounds.size.width - width) / 2, (self.containerView.bounds.size.height - height) / 2, width, height);
  74. return containerViewBounds;
  75. }
  76. // 建议就这样重写就行,这个应该是控制器内容大小变化时,就会调用这个方法, 比如适配横竖屏幕时,翻转屏幕时
  77. // 可以使用UIContentContainer的方法来调整任何子视图控制器的大小或位置。
  78. - (void)preferredContentSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container {
  79. [super preferredContentSizeDidChangeForChildContentContainer:container];
  80. if (container == self.presentedViewController) [self.containerView setNeedsLayout];
  81. }
  82. - (void)containerViewWillLayoutSubviews {
  83. [super containerViewWillLayoutSubviews];
  84. self.dimmingView.frame = self.containerView.bounds;
  85. }
  86. @end
  87. @interface AdViewController () <UIViewControllerTransitioningDelegate>
  88. @end
  89. @implementation AdViewController
  90. - (instancetype)init {
  91. if (self = [super init]) {
  92. self.transitioningDelegate = self;
  93. self.modalPresentationStyle = UIModalPresentationCustom;
  94. }
  95. return self;
  96. }
  97. - (void)viewDidLoad {
  98. [super viewDidLoad];
  99. self.view.backgroundColor = [UIColor whiteColor];
  100. self.view.layer.cornerRadius = 14;
  101. self.view.clipsToBounds = YES;
  102. self.img = [[UIImageView alloc] init];
  103. self.img.contentMode = UIViewContentModeScaleAspectFill;
  104. self.img.clipsToBounds = YES;
  105. [self.view addSubview:self.img];
  106. [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
  107. make.left.top.right.equalTo(self.view);
  108. make.height.mas_equalTo(210);
  109. }];
  110. self.label = [[UILabel alloc] init];
  111. self.label.textAlignment = NSTextAlignmentCenter;
  112. [self.view addSubview:self.label];
  113. [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
  114. make.left.equalTo(self.view).offset(25);
  115. make.right.equalTo(self.view).offset(-25);
  116. make.top.equalTo(self.img.mas_bottom).offset(16);
  117. make.height.mas_equalTo(40);
  118. }];
  119. self.label.numberOfLines = 0;
  120. self.label.font = [UIFont systemFontOfSize:14];
  121. self.label.textColor = [UIColor blackColor];
  122. UIButton *detailBtn = [[UIButton alloc] init];
  123. [self.view addSubview:detailBtn];
  124. [detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  125. make.left.equalTo(self.view).offset(25);
  126. make.right.equalTo(self.view).offset(-25);
  127. make.top.equalTo(self.label.mas_bottom).offset(20);
  128. make.height.mas_equalTo(40);
  129. }];
  130. [detailBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  131. [detailBtn setTitle:@"立即查看" forState:UIControlStateNormal];
  132. detailBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
  133. detailBtn.layer.backgroundColor = [UIColor colorWithRed:1.f green:64 / 255.f blue:149 / 255.f alpha:1.0].CGColor;
  134. detailBtn.layer.cornerRadius = 22;
  135. detailBtn.layer.shadowColor = [UIColor colorWithRed:1.f green:64 / 255.f blue:149 / 255.f alpha:0.36].CGColor;
  136. detailBtn.layer.shadowOffset = CGSizeMake(0, 8);
  137. detailBtn.layer.shadowOpacity = 1;
  138. detailBtn.layer.shadowRadius = 10;
  139. [detailBtn addTarget:self action:@selector(detail) forControlEvents:UIControlEventTouchUpInside];
  140. UIButton *cancelBtn = [[UIButton alloc] init];
  141. [self.view addSubview:cancelBtn];
  142. [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  143. make.centerX.equalTo(self.view);
  144. make.top.equalTo(detailBtn.mas_bottom).offset(20);
  145. }];
  146. cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  147. [cancelBtn setTitle:@"稍后再说" forState:UIControlStateNormal];
  148. [cancelBtn setTitleColor:[UIColor colorWithHexString:@"#8F9294"] forState:UIControlStateNormal];
  149. [cancelBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
  150. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", imageURl, self.model[@"banner"]]];
  151. [self.img sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"jiazai"]];
  152. self.label.text = self.model[@"Caption"];
  153. }
  154. - (void)detail {
  155. [self dismissViewControllerAnimated:YES completion:^{
  156. // WebViewController *webVC = [[WebViewController alloc] init];
  157. // webVC.url = self.model[@"url"];
  158. // webVC.activityPK = self.model[@"PK"];
  159. // webVC.hidesBottomBarWhenPushed = YES;
  160. // [self.navigationController pushViewController:webVC animated:YES];
  161. // [[self findViewController].navigationController pushViewController:webVC animated:YES];
  162. if (self.callback) {
  163. self.callback();
  164. }
  165. }];
  166. }
  167. - (void)dismiss {
  168. [self dismissViewControllerAnimated:YES completion:nil];
  169. }
  170. - (UIViewController *)findViewController {
  171. id target = self;
  172. while (target) {
  173. target = ((UIResponder *) target).nextResponder;
  174. if ([target isKindOfClass:[UIViewController class]]) {
  175. break;
  176. }
  177. }
  178. return target;
  179. }
  180. - (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source {
  181. return [[AdPresenter alloc] initWithPresentedViewController:presented presentingViewController:self];
  182. }
  183. @end