UpdateViewController.m 8.4 KB

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