前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自定义转场动画

自定义转场动画

作者头像
码客说
发布2019-10-22 14:11:23
4890
发布2019-10-22 14:11:23
举报
文章被收录于专栏:码客码客

实现思路

自定义转场动画时需要做以下几步 以下内容假设是从A–>B添加的segue

  • 添加两个segue(一个是用于正向转场 ,一个是新页面推出时的反向转场动画)
  • 从原view向目标view右键拖动 这是segue的可选项中就会有新添加的两个segue 选择正向的那个转场,也可以选择custom 然后设置segue对应的class
  • 反向转场相对就要麻烦些了 反向转场是B–>A 首先在A中重写返回A时调用的方法(不是B中)方法中设置转场调用的动画

详细实现

正向转场的实现类

代码语言:javascript
复制
//
//  PushSegue.swift
//  signDemo
//
//  Created by PSVMC on 15/6/9.
//  Copyright (c) 2015年 PSVMC. All rights reserved.
//

import UIKit

class CustomPushSegue: UIStoryboardSegue {

    override func perform() {
        //原视图
        var source = self.sourceViewController as! UIViewController;
        //目标视图
        var destination = self.destinationViewController as! UIViewController;

        source.view.addSubview(destination.view);
        destination.view.frame=source.view.frame;

        destination.view.frame.origin.x+=320;
        //destination.view.transform=CGAffineTransformMakeScale(0.1, 0.1)


        source.view.alpha=CGFloat(1.0)
        destination.view.alpha=CGFloat(1.0);

        UIView.animateKeyframesWithDuration(0.3, delay: 0, options: UIViewKeyframeAnimationOptions.CalculationModeCubic, animations: {
            source.view.frame.origin.x-=320;
            destination.view.alpha=1.0;
            destination.view.transform=CGAffineTransformMakeScale(1.0, 1.0)
            //destination.view.frame.origin.x-=320;
            }, completion:{(finish) -> Void in
                source.presentViewController(destination, animated: false, completion: nil)
            }
        )
    }
}

反向转场的实现类

代码语言:javascript
复制
//
//  CustomPushUnwindSegue.swift
//  signDemo
//
//  Created by PSVMC on 15/6/9.
//  Copyright (c) 2015年 PSVMC. All rights reserved.
//

import UIKit

class CustomPushUnwindSegue: UIStoryboardSegue {
    override func perform() {
        var source = self.sourceViewController as! UIViewController;
        var destination = self.destinationViewController as! UIViewController;

        let window = UIApplication.sharedApplication().keyWindow
        window?.insertSubview(destination.view, aboveSubview: source.view)

        destination.view.frame=source.view.frame;

        destination.view.bounds.origin.x-=320;
        //destination.view.transform=CGAffineTransformMakeScale(0.1, 0.1)
        //bounds是绝对位置
        //frame是相对父元素的位置


        //source.view.bounds.origin.x-=320;
        source.view.alpha=CGFloat(1.0)
        destination.view.alpha=CGFloat(1.0);

        UIView.animateKeyframesWithDuration(0.3, delay: 0, options: UIViewKeyframeAnimationOptions.CalculationModeCubic, animations: {

            source.view.bounds.origin.x+=320;
            destination.view.bounds.origin.x+=320;
            destination.view.alpha=1.0;
            destination.view.transform=CGAffineTransformMakeScale(1.0, 1.0)
            //destination.view.frame.origin.x-=320;
            }, completion:{(finish) -> Void in
                source.dismissViewControllerAnimated(false, completion: nil);
            }
        )

    }
}

重写反向转场的动画

代码语言:javascript
复制
override func segueForUnwindingToViewController(toViewController: UIViewController, fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue {
    if let id = identifier{
        if id == "unwindToLogin" {
            let unwindSegue = CustomPushUnwindSegue(identifier: id, source: fromViewController, destination: toViewController, performHandler: { () -> Void in

            })
            return unwindSegue
        }
    }

    return super.segueForUnwindingToViewController(toViewController, fromViewController: fromViewController, identifier: identifier)
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-06-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现思路
  • 详细实现
    • 正向转场的实现类
      • 反向转场的实现类
        • 重写反向转场的动画
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档