自定义幻灯片Segue - Xcode 8.0 Swift 3.0

时间:2016-10-24 10:44:42

标签: swift xcode animation segue

创建自定义segue的最佳方法是什么,让我按下按钮,然后滑动视图控制器。我创建了一个从左到右的segue,但我想让它走另一条路。我查看了一些Youtube视频和this question,但他们没有告诉我我想要的内容。

我的代码:

import UIKit

class SegueFromLeft: UIStoryboardSegue
{
    override func perform()
    {
        let src = self.sourceViewController
        let dst = self.destinationViewController

        src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
        dst.view.transform = CGAffineTransformMakeTranslation(-src.view.frame.size.width, 0)

        UIView.animateWithDuration(0.25,
            delay: 0.0,
            options: UIViewAnimationOptions.CurveEaseInOut,
            animations: {
                dst.view.transform = CGAffineTransformMakeTranslation(0, 0)
            },
            completion: { finished in
                src.presentViewController(dst, animated: false, completion: nil)
            }
        )
    }
}

基本上,我想创建一个从右到左的segue。

谢谢

我正在使用Xcode 8.0和Swift 3.0

2 个答案:

答案 0 :(得分:4)

试试这个:

class SegueFromLeft: UIStoryboardSegue{
    override func perform(){
        let src=sourceViewController
        let dst=destinationViewController
        let slide_view=destinationViewController.view

        src.view.addSubview(slide_view)
        slide_view.transform=CGAffineTransform.init(translationX: src.view.frame.size.width, 0)

        UIView.animateWithDuration(0.25,
            delay: 0.0,
            options: UIViewAnimationOptions.CurveEaseInOut,
            animations: {
                slide_view.transform=CGAffineTransform.identity
        }, completion: {finished in
                src.present(dst, animated: false, completion: nil)
                slide_view.removeFromSuperview()
        })
    }
}

答案 1 :(得分:0)

Swift 3.1:

class ProceedToAppStoryboardSegue: UIStoryboardSegue {

    override func perform(){

        let slideView = destination.view

        source.view.addSubview(slideView!)
        slideView?.transform = CGAffineTransform(translationX: source.view.frame.size.width, y: 0)

        UIView.animate(withDuration: 0.25,
                               delay: 0.0,
                               options: UIViewAnimationOptions.curveEaseInOut,
                               animations: {
                               slideView?.transform = CGAffineTransform.identity
        }, completion: { finished in

            self.source.present(self.destination, animated: false, completion: nil)
            slideView?.removeFromSuperview()
        })
    }
}