美文网首页
转场做侧滑

转场做侧滑

作者: 蛋白质corn | 来源:发表于2017-08-16 14:15 被阅读0次

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];

}

相关文章

  • 转场做侧滑

    1.设置第二控制器转场代理 #import "ViewController.h" #import "SecondV...

  • 侧滑的转场实现

    学习了seedante大神的《iOS 视图控制器转场详解》https://github.com/seedante/...

  • swift实现简易的侧滑转场

    一.实现效果如下: 二.如何使用: 将"SideTransitionAnimator"类文件拖入工程中,在触发...

  • View的滑动

    View滑动简介 现在Android设备中,滑动可以说是无处不在,Activity转场,侧滑菜单,下拉刷新,上拉加...

  • iOS侧滑菜单-完全解耦-一行调用

    DYLeftSlipManager 一行代码实现侧滑。完全解耦,两个界面之间不需要有任何耦合。基于控制器自定义转场...

  • 侧滑

  • 侧滑

  • 侧滑

    1.全屏 2.去DrawerLayout阴影 3.设置DrawerLayout主页面移动 4.Toolbar me...

  • 侧滑

    #import "LeftViewController.h" #import "UIViewController+...

  • 侧滑

网友评论

      本文标题:转场做侧滑

      本文链接:https://www.haomeiwen.com/subject/jgsirxtx.html