在Swift中设置自定义segue方式(从顶部到按钮方向)

时间:2016-06-18 16:40:41

标签: ios xcode swift storyboard segue

我想在故事板中的两个场景之间建立一个segue,其行为与“Apple Keynote”效果移动(从上到下方向)完全相同。现在我知道有一个称为模态segue的segue,但它在底部到顶部方向起作用。

请帮助我!

我制作了一个关于我想要什么类型的视频的视频,请查看它! https://www.dropbox.com/s/zqyxrm638ellfbx/whatiwant.mov?dl=0

2 个答案:

答案 0 :(得分:4)

您可以像这样实现自定义UIStoryboardSegue

class TopDownSegue: UIStoryboardSegue {
    let duration: NSTimeInterval = 1
    let delay: NSTimeInterval = 0
    let animationOptions: UIViewAnimationOptions = [.CurveEaseInOut]

    override func perform() {
        // get views
        let sourceView = sourceViewController.view
        let destinationView = destinationViewController.view

        // get screen height
        let screenHeight = UIScreen.mainScreen().bounds.size.height
        destinationView.transform = CGAffineTransformMakeTranslation(0, -screenHeight)

        // add destination view to view hierarchy
        UIApplication.sharedApplication().keyWindow?.insertSubview(destinationView, aboveSubview: sourceView)

        // animate
        UIView.animateWithDuration(duration, delay: delay, options: animationOptions, animations: { 
            destinationView.transform = CGAffineTransformIdentity
            }) { (_) in
                self.sourceViewController.presentViewController(self.destinationViewController, animated: false, completion: nil)
        }
    }
}

并在故事板中使用新的自定义segue:

custom segue

答案 1 :(得分:0)

由于接受的代码已过时,因此此处为更新版本:

class SegueFromTop: UIStoryboardSegue {
    override func perform() {
        let src = self.source
        let dst = self.destination

        src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
        dst.view.transform = CGAffineTransform(translationX: 0, y: -src.view.frame.size.height)

        UIView.animate(withDuration: 0.25,
                       delay: 0.0,
                       options: .curveEaseInOut,
                       animations: {
                        dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
        },
                       completion: { finished in
                        src.present(dst, animated: false, completion: nil)
        }
        )
    }
}