UIPresentationController自定义转场动画

在APP开发过程中,总要设计到控制器的跳转。在IOS8中,苹果推出了UIPresentationController来管理所有modal的控制器。在这里,我们首选回顾一下,系统自带的modal转场动画效果。modalTransitionStyle分别有已下四种效果:

/*
 UIModalTransitionStyleCoverVertical 默认效果:自下而上
 UIModalTransitionStyleFlipHorizontal 翻转效果
 UIModalTransitionStyleCrossDissolve 淡出效果
 UIModalTransitionStylePartialCurl 上下翻页效果
 */

这个在使用的过程中,仅仅需要修改需要切换控制器的modalTransitionStyle分隔即可。
例如,单击主控制器的跳转到下一个控制器的实现

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    //初始化要切换的控制器
    UIViewController *vc = [[UIViewController alloc] init];

    //设置其背景颜色
         vc.view.backgroundColor = [UIColor redColor];

         // 设置动画样式(系统自带的调整方式四种)
        vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    //控制器跳转
        [self presentViewController:vc animated:YES completion:nil];
}

而到了IOS8以后,苹果推出UIPresentationController可以实现自定义的控制器切换效果,下面我们具体的来学习使用一下。

我们采用一个小demo示例来对其进行演示,首次采用了自动布局的来搭建界面。如下图所示:
效果演示
其中前四个转场效果为苹果系统自带的,如前面讲到的只需要修改modalTransitionStyle的值即可。最后一个效果是自定义转场实现出来的效果,下面我们来进行具体的阐述
在单击了“自定义左右效果”按钮后,需要设置其 设置展示样式为自定义样式

vc.modalPresentationStyle = UIModalPresentationCustom;

除此之外,还需要三个对象来实现自定义过渡,一个UIPresentationController 的子类、一个遵从 UIViewControllerAnimatedTransitioning 协议的类以及遵从UIViewControllerTransitioningDelegate协议的类。

具体代码为:

//在ViewController中点击事件实现:
//要切换的控制器
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    //初始化要切换的控制器
    UIViewController *vc = [[UIViewController alloc] init];

    //设置其背景颜色
    vc.view.backgroundColor = [UIColor redColor];

    //设置展示样式(自定义)
    vc.modalPresentationStyle = UIModalPresentationCustom;

    //设置代理(设置UIPresentationController)(为实现UIViewControllerTransitioningDelegate协议的类)
    vc.transitioningDelegate = [Transition sharedtransition];

    //控制器跳转
    [self presentViewController:vc animated:YES completion:nil];
}

需要自定义转场动画后,需要实现要调整控制器的transitioningDelegate代理。创建实现UIViewControllerTransitioningDelegate协议的类(这里我们命名为Transition (并且将其设置成为单例)),在这个类中实现UIViewControllerTransitioningDelegate协议方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值