// // AdViewController.m // 千模 // // Created by Drew on 2019/1/4. // Copyright © 2019 MUMEI. All rights reserved. // #import "AdViewController.h" #import "Masonry.h" #import "WebViewController.h" @interface AdPresenter : UIPresentationController @property(nonatomic, strong) UIView *dimmingView; @end @implementation AdPresenter : 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 transitionCoordinator = self.presentingViewController.transitionCoordinator; // 动画期间,背景View的动画方式 self.dimmingView.alpha = 0.f; [transitionCoordinator animateAlongsideTransition:^(id context) { self.dimmingView.alpha = 0.4f; } completion:NULL]; } #pragma mark 点击了背景遮罩view - (void)dimmingViewTapped:(UITapGestureRecognizer *)sender { [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL]; } // 在呈现过渡结束时被调用的,并且该方法提供一个布尔变量来判断过渡效果是否完成 - (void)presentationTransitionDidEnd:(BOOL)completed { // 在取消动画的情况下,可能为NO,这种情况下,应该取消视图的引用,防止视图没有释放 if (!completed) { self.dimmingView = nil; } } // 消失过渡即将开始的时候被调用的 - (void)dismissalTransitionWillBegin { id transitionCoordinator = self.presentingViewController.transitionCoordinator; [transitionCoordinator animateAlongsideTransition:^(id 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 = ScreenWidth - 96; 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 )container { [super preferredContentSizeDidChangeForChildContentContainer:container]; if (container == self.presentedViewController) [self.containerView setNeedsLayout]; } - (void)containerViewWillLayoutSubviews { [super containerViewWillLayoutSubviews]; self.dimmingView.frame = self.containerView.bounds; } @end @interface AdViewController () @end @implementation AdViewController - (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 = YES; self.img = [[UIImageView alloc] init]; self.img.contentMode = UIViewContentModeScaleAspectFill; self.img.clipsToBounds = YES; [self.view addSubview:self.img]; [self.img mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.equalTo(self.view); make.height.mas_equalTo(210); }]; self.label = [[UILabel alloc] init]; self.label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.label]; [self.label mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offset(25); make.right.equalTo(self.view).offset(-25); make.top.equalTo(self.img.mas_bottom).offset(16); make.height.mas_equalTo(40); }]; self.label.numberOfLines = 0; self.label.font = [UIFont systemFontOfSize:14]; self.label.textColor = [UIColor blackColor]; UIButton *detailBtn = [[UIButton alloc] init]; [self.view addSubview:detailBtn]; [detailBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offset(25); make.right.equalTo(self.view).offset(-25); make.top.equalTo(self.label.mas_bottom).offset(20); make.height.mas_equalTo(40); }]; [detailBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [detailBtn setTitle:@"立即查看" forState:UIControlStateNormal]; detailBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; detailBtn.layer.backgroundColor = [UIColor colorWithRed:1.f green:64 / 255.f blue:149 / 255.f alpha:1.0].CGColor; detailBtn.layer.cornerRadius = 22; detailBtn.layer.shadowColor = [UIColor colorWithRed:1.f green:64 / 255.f blue:149 / 255.f alpha:0.36].CGColor; detailBtn.layer.shadowOffset = CGSizeMake(0, 8); detailBtn.layer.shadowOpacity = 1; detailBtn.layer.shadowRadius = 10; [detailBtn addTarget:self action:@selector(detail) forControlEvents:UIControlEventTouchUpInside]; UIButton *cancelBtn = [[UIButton alloc] init]; [self.view addSubview:cancelBtn]; [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.view); make.top.equalTo(detailBtn.mas_bottom).offset(20); }]; cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14]; [cancelBtn setTitle:@"稍后再说" forState:UIControlStateNormal]; [cancelBtn setTitleColor:[UIColor colorWithHexString:@"#8F9294"] forState:UIControlStateNormal]; [cancelBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", imageURl, self.model[@"banner"]]]; [self.img sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"jiazai"]]; self.label.text = self.model[@"Caption"]; } - (void)detail { [self dismissViewControllerAnimated:YES completion:^{ // WebViewController *webVC = [[WebViewController alloc] init]; // webVC.url = self.model[@"url"]; // webVC.activityPK = self.model[@"PK"]; // webVC.hidesBottomBarWhenPushed = YES; // [self.navigationController pushViewController:webVC animated:YES]; // [[self findViewController].navigationController pushViewController:webVC animated:YES]; if (self.callback) { self.callback(); } }]; } - (void)dismiss { [self dismissViewControllerAnimated:YES completion:nil]; } - (UIViewController *)findViewController { id target = self; while (target) { target = ((UIResponder *) target).nextResponder; if ([target isKindOfClass:[UIViewController class]]) { break; } } return target; } - (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source { return [[AdPresenter alloc] initWithPresentedViewController:presented presentingViewController:self]; } @end