虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > IOS编程/Objective-C > iOS 控制器自定义动画跳转方法(模态跳转)

iOS 控制器自定义动画跳转方法(模态跳转)
类别:IOS编程/Objective-C   作者:码皇   来源:互联网   点击:

下面小编就为大家分享一篇iOS 控制器自定义动画跳转方法(模态跳转),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

参考资料:

Apple 开发文档 Customizing the Transition Animations

WWDC 2013 Custom Transitions Using View Controllers

图例:

跳转的动画有很多,全部可以自定义

创建自定义跳转必须遵循的三个步骤:

1、创建一个类,并实现了 UIViewControllerAnimatedTransitioning 协议

2、创建一个类作为 UIViewControllerTransitioningDelegate 过渡代理

3、在模态跳转前修改控制器的 transitioningDelegate 代理为自定义的代理(步骤2的代理类)

核心代码示例

一、创建一个类,并实现了 UIViewControllerAnimatedTransitioning 协议

这个协议主要控制控制器视图的显示的,通过 transitionContext 可以获取到每个视图和控制器,并进行动画的设置

    class AnimatedTransitioning: NSObject {
    var isPresenting: Bool = false}
    extension AnimatedTransitioning: UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return 0.5 }
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let fromView = transitionContext.view(forKey: .from)! let toView = transitionContext.view(forKey: .to)! let containerView = transitionContext.containerView if isPresenting {
    toView.transform = CGAffineTransform(scaleX: 0, y: 0) containerView.addSubview(toView) }
    else {
    containerView.insertSubview(toView, belowSubview: fromView) }
    UIView.animate(withDuration: 0.5, animations: {
    if self.isPresenting {
    toView.transform = CGAffineTransform.identity }
    else {
    fromView.transform = CGAffineTransform(scaleX: 0.001, y: 0.001) }
    }
    ) {
    (finished) in transitionContext.completeTransition(finished) }
    }
    }

二、创建一个类作为 UIViewControllerTransitioningDelegate 过渡代理

这里设置 presented 和 dismissed 时各自的动画转换类,可以设置为不同的类

    class CustomTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    let at = AnimatedTransitioning() at.isPresenting = true return at }
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    let at = AnimatedTransitioning() at.isPresenting = false return at }
    }

三、在模态跳转前修改控制器的 transitioningDelegate 代理为自定义的代理

注意:代理不能为局部变量

    class ViewController: UIViewController {
    // 必须保存为实例变量 var ctDelegate = CustomTransitioningDelegate() override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let vc = TempViewController() vc.transitioningDelegate = ctDelegate self.present(vc, animated: true, completion: nil) }
    }

以上这篇iOS 控制器自定义动画跳转方法(模态跳转)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

  • IOS动画效果源代码整理(粒子、雪花、火焰、河流、蒸汽)
  • iOS Swift控制器转场动画示例代码
  • iOS如何自定义控制器转场动画push详解
  • iOS动画解析之圆球加载动画XLBallLoading的实现
  • iOS如何为导航栏添加播放动画
  • IOS 中动画的暂停与继续播放的详解
  • IOS登录页面动画、转场动画开发详解
相关热词搜索: iOS 控制器 自定义 动画 跳转