1.设置第二控制器转场代理
#import "ViewController.h"
#import "SecondViewController.h"
#import "Animal.h"
@interface ViewController ()
@property(nonatomic,strong)UIButton *bt;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.bt];
}
-(UIButton *)bt{
if (!_bt) {
_bt = [UIButton buttonWithType:UIButtonTypeCustom];
_bt.frame = CGRectMake(100, 100, 50, 50);
[_bt setTitle:@"筛选" forState:UIControlStateNormal];
[_bt setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[_bt addTarget:self action:@selector(btClick) forControlEvents:UIControlEventTouchUpInside];
}
return _bt;
}
-(void)btClick
{
//遵守转场代理
SecondViewController *VC = [SecondViewController new];
Animal *animal = [Animal shareInstance];
VC.modalPresentationStyle = UIModalPresentationCustom;
VC.transitioningDelegate = animal;
//设置第二控制器的view的frame
animal.presentFame = CGRectMake(40, 0, [[UIScreen mainScreen] bounds].size.width-40, [[UIScreen mainScreen] bounds].size.height);
[self presentViewController:VC animated:YES completion:nil];
}
2.单独封装一个类Animal管理转场动画
#import "Animal.h"#import "SecondViewController.h"#import "MyPresentVc.h"@interface Animal()@property(nonatomic,assign)BOOL isPresent;
@end
@implementation Animal
+(Animal*)shareInstance{
static Animal *animal = nil;
if (animal==nil) {
animal = [[Animal alloc]init];
}
return animal;
}
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
//改变view的大小 MyPresentVc *presentVc = [[MyPresentVc alloc]initWithPresentedViewController:presented presentingViewController:presenting]; presentVc.presentFrame = _presentFame; return presentVc;
}
//推出时走的代理- (nullable id)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{ _isPresent = YES; return self;}
//退出时走的代理- (nullable id)animationControllerForDismissedController:(UIViewController *)dismissed{ _isPresent = NO; return self;}//动画时间- (NSTimeInterval)transitionDuration:(nullable id)transitionContext
{
return 1.0;
}
//通过获取view上下文做相关动画- (void)animateTransition:(id)transitionContex{ //UIViewController *presentVC = [transitionContex viewControllerForKey:UITransitionContextToViewControllerKey]; if (_isPresent) { [self presentWith:transitionContex]; } else{ [self dismissWith:transitionContex]; } }
-(void)presentWith:(id)transitionContex{ UIView *presentView = [transitionContex viewForKey:UITransitionContextToViewKey]; [transitionContex.containerView addSubview:presentView]; NSLog(@"%@",NSStringFromCGRect(presentView.frame)); //缩放 CGAffineTransform form1 = CGAffineTransformMakeScale(0.5, 0.5); presentView.transform = CGAffineTransformTranslate(form1, [[UIScreen mainScreen] bounds].size.width, 0); //平移 //presentView.transform = CGAffineTransformMakeTranslation([[UIScreen mainScreen] bounds].size.width, 0); [UIView animateWithDuration:1 animations:^{ //还原 presentView.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { [transitionContex completeTransition:YES]; }];}
-(void)dismissWith:(id)transitionContex
{
UIView *dismissView = [transitionContex viewForKey:UITransitionContextFromViewKey];
[transitionContex.containerView addSubview:dismissView];
[UIView animateWithDuration:1 animations:^{
dismissView.transform = CGAffineTransformMakeTranslation([[UIScreen mainScreen] bounds].size.width, 0);
} completion:^(BOOL finished) {
[dismissView removeFromSuperview];
[transitionContex completeTransition:YES];
}];
}
3. MyPresentVc 继承 UIPresentationController ,通过这个类来改变第二控制器的view的frame
#import "MyPresentVc.h"
@implementation MyPresentVc
-(void)containerViewWillLayoutSubviews{
[super containerViewWillLayoutSubviews];
self.presentedView.frame = _presentFrame;
_bgView = [[UIView alloc]initWithFrame:self.containerView.bounds];
_bgView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.2];
[self.containerView addSubview:_bgView];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(disMissVc)];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(disMissVc)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[_bgView addGestureRecognizer:swipe];
[_bgView addGestureRecognizer:tap];
}
- (void)disMissVc{
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}