| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- //
- // UpdateViewController.m
- // 千模
- //
- // Created by Drew on 2019/1/4.
- // Copyright © 2019 MUMEI. All rights reserved.
- //
- #import "UpdateViewController.h"
- #import "Masonry.h"
- @interface UpdatePresenter : UIPresentationController
- @property(nonatomic, strong) UIView *dimmingView;
- @end
- @implementation UpdatePresenter : UIPresentationController
- - (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController {
- self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController];
- if (self) {
- // 必须设置 presentedViewController 的 modalPresentationStyle
- // 在自定义动画效果的情况下,苹果强烈建议设置为 UIModalPresentationCustom
- presentedViewController.modalPresentationStyle = UIModalPresentationCustom;
- }
- return self;
- }
- // 呈现过渡即将开始的时候被调用的
- // 可以在此方法创建和设置自定义动画所需的view
- - (void)presentationTransitionWillBegin {
- self.containerView.layer.cornerRadius = 14;
- // 背景遮罩
- UIView *dimmingView = [[UIView alloc] initWithFrame:self.containerView.bounds];
- dimmingView.backgroundColor = [UIColor blackColor];
- dimmingView.opaque = NO; //是否透明
- dimmingView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- [dimmingView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dimmingViewTapped:)]];
- self.dimmingView = dimmingView;
- [self.containerView addSubview:dimmingView]; // 添加到动画容器View中。
- // 获取presentingViewController 的转换协调器,应该动画期间的一个类?上下文?之类的,负责动画的一个东西
- id <UIViewControllerTransitionCoordinator> transitionCoordinator = self.presentingViewController.transitionCoordinator;
- // 动画期间,背景View的动画方式
- self.dimmingView.alpha = 0.f;
- [transitionCoordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context) {
- self.dimmingView.alpha = 0.4f;
- } completion:NULL];
- }
- #pragma mark 点击了背景遮罩view
- - (void)dimmingViewTapped:(UITapGestureRecognizer *)sender {
- }
- // 在呈现过渡结束时被调用的,并且该方法提供一个布尔变量来判断过渡效果是否完成
- - (void)presentationTransitionDidEnd:(BOOL)completed {
- // 在取消动画的情况下,可能为NO,这种情况下,应该取消视图的引用,防止视图没有释放
- if (!completed) {
- self.dimmingView = nil;
- }
- }
- // 消失过渡即将开始的时候被调用的
- - (void)dismissalTransitionWillBegin {
- id <UIViewControllerTransitionCoordinator> transitionCoordinator = self.presentingViewController.transitionCoordinator;
- [transitionCoordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context) {
- self.dimmingView.alpha = 0.f;
- } completion:NULL];
- }
- // 消失过渡完成之后调用,此时应该将视图移除,防止强引用
- - (void)dismissalTransitionDidEnd:(BOOL)completed {
- if (completed == YES) {
- [self.dimmingView removeFromSuperview];
- self.dimmingView = nil;
- }
- }
- // 返回目标控制器Viewframe
- - (CGRect)frameOfPresentedViewInContainerView {
- CGFloat width = 280;
- CGFloat height = 386.f;
- CGRect containerViewBounds = CGRectMake((self.containerView.bounds.size.width - width) / 2, (self.containerView.bounds.size.height - height) / 2, width, height);
- return containerViewBounds;
- }
- // 建议就这样重写就行,这个应该是控制器内容大小变化时,就会调用这个方法, 比如适配横竖屏幕时,翻转屏幕时
- // 可以使用UIContentContainer的方法来调整任何子视图控制器的大小或位置。
- - (void)preferredContentSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container {
- [super preferredContentSizeDidChangeForChildContentContainer:container];
- if (container == self.presentedViewController) [self.containerView setNeedsLayout];
- }
- - (void)containerViewWillLayoutSubviews {
- [super containerViewWillLayoutSubviews];
- self.dimmingView.frame = self.containerView.bounds;
- }
- @end
- @interface UpdateViewController () <UIViewControllerTransitioningDelegate>
- @end
- @implementation UpdateViewController
- - (instancetype)init {
- if (self = [super init]) {
- self.transitioningDelegate = self;
- self.modalPresentationStyle = UIModalPresentationCustom;
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- self.view.layer.cornerRadius = 14;
- self.view.clipsToBounds = NO;
- UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_xinbanben"]];
- [self.view addSubview:img];
- [img mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.equalTo(self.view);
- make.top.equalTo(self.view).offset(-27);
- make.height.mas_equalTo(227);
- }];
- UILabel *newVersion = [[UILabel alloc] init];
- newVersion.text = @"发现新版本";
- newVersion.font = [UIFont systemFontOfSize:19 weight:UIFontWeightMedium];
- newVersion.textColor = [UIColor whiteColor];
- [self.view addSubview:newVersion];
- [newVersion mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.view).offset(20);
- make.top.equalTo(self.view).offset(26);
- }];
- UIButton *version = [[UIButton alloc] init];
- version.backgroundColor = [UIColor whiteColor];
- version.layer.cornerRadius = 9;
- [version setTitle:self.version forState:UIControlStateNormal];
- [version setTitleColor:[UIColor colorWithHexString:@"#FF4095"] forState:UIControlStateNormal];
- version.titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
- [self.view addSubview:version];
- [version mas_makeConstraints:^(MASConstraintMaker *make) {
- make.width.mas_equalTo(44);
- make.height.mas_equalTo(18);
- make.left.equalTo(self.view).offset(20);
- make.top.equalTo(self.view).offset(54);
- }];
- UILabel *label = [[UILabel alloc] init];
- label.font = [UIFont systemFontOfSize:14];
- label.textColor = [UIColor colorWithHexString:@"#4D4D4D"];
- label.numberOfLines = 0;
- label.text = self.desc ? self.desc : @"请立即更新";
- [self.view addSubview:label];
- [label mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.view).offset(40);
- make.right.equalTo(self.view).offset(-40);
- make.top.equalTo(self.view).offset(175);
- }];
- UIButton *button = [[UIButton alloc] init];
- [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [button setTitle:@"立即更新" forState:UIControlStateNormal];
- button.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
- button.layer.backgroundColor = [UIColor colorWithRed:1.0 green:64 / 255.f blue:149 / 255.f alpha:1.0].CGColor;
- button.layer.cornerRadius = 22;
- button.layer.shadowColor = [UIColor colorWithRed:1.0 green:64 / 255.f blue:149 / 255.f alpha:0.36].CGColor;
- button.layer.shadowOffset = CGSizeMake(0, 8);
- button.layer.shadowOpacity = 1;
- button.layer.shadowRadius = 10;
- [self.view addSubview:button];
- [button mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.view).offset(20);
- make.right.equalTo(self.view).offset(-20);
- make.bottom.equalTo(self.view.mas_bottom).offset(-25);
- make.height.mas_equalTo(44);
- }];
- [button addTarget:self action:@selector(update) forControlEvents:UIControlEventTouchUpInside];
- }
- - (void)update {
- NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1434011196?mt=8"];
- if (@available(iOS 10.0, *)) {
- [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
- } else {
- [[UIApplication sharedApplication] openURL:url];
- }
- }
- - (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source {
- return [[UpdatePresenter alloc] initWithPresentedViewController:presented presentingViewController:self];
- }
- @end
|