如何在没有 Storyboards / Unwindng Segues 的情况下在 Swift 中使用 UIViewControllerAnimatedTransitioning?

How to use UIViewControllerAnimatedTransitioning in Swift without Storyboards / Unwindng Segues?

我在 Swift 中创建自定义视图控制器转换时遇到了一些问题。我找到了这个优秀的教程 http://mathewsanders.com/interactive-transitions-in-swift/ 但我无法弄清楚如何在没有情节提要的情况下创建过渡(我正在开发的应用程序不使用界面生成器或情节提要)。


所需结果的 GIF

http://mathewsanders.com/assets/transitions-3/Menu-3.gif


具体来说,我遇到的问题是在使用 segues 的这一部分:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

let menu = segue.destinationViewController as MenuViewController
menu.transitioningDelegate = self.transitionManager

}

@IBAction func unwindToMainViewController (sender: UIStoryboardSegue){
    // bug? exit segue doesn't dismiss so we do it manually... 
    self.dismissViewControllerAnimated(true, completion: nil)

} 

如果你想放弃 segue 和 Storyboard 路线,你如何创建这样的过渡?

我的目标是使用平移手势向下滑动 UICollectionView 详细视图以关闭(类似于 Facebook 照片关闭它/关闭 viewcontroller 的方式,我知道 Facebook 为此使用滚动视图,但我需要使用 UICollectionView 和充当单元格详细视图的 UIViewController)。

所以您需要做的是将您的代码放入平移手势识别器的事件处理程序中。当手势识别器的状态更改为 UIGestureRecognizerStateBegan 时,您需要使用 UIViewController 的 presentViewController:animated: 方法开始转换。

class MyViewController: UIViewController {
    var transitionPercentage: CGFloat = 0.0

    func handlePanGestureRecognizer(gestureRecognizer: UIPanGestureRecognizer) {
        let gestureTranslationX: CGFloat = gestureRecognizer.translationInView(view).x
        switch gestureRecognizer.state {
            case .Began: {
                let menuViewController: MenuViewController = MenuViewController()
                menuViewController.transitioningDelegate = self.interactiveTransitionManager;
                presentViewController(menuViewController, animated: true, completion: nil)
            }
            case .Changed: {
                //Here you are going to want to figure out the percentage of your interactive transition. Feed it a threshold of how far you want the finger to move before finishing the transition, and then calculate a percentage for it using our gestureTranslationX variable above.
                transitionPercentage: CGFloat = gestureTranslationX / thresholdValue; //threshold value should be something like self.view.frame.size.width. Our percentage should be no less than 0.0 and no greater than 0.999999. You should probably have logic that prevents this value from violating either one.
                interactiveTransitionManager.updateInteractiveTransition(transitionPercentage) //transitionPercentage is whatever CGFloat variable you use to track the pan state from 0.0 to 1.0
            }
            case .Cancelled, .Ended: {
                //Here you can put logic that uses the transitionPercentage to figure out whether to complete the transition or cancel it.
                if transitionPercentage >= 0.5 {
                    transitionManager.finishInteractiveTransition()
                } else {
                    transitionManager.cancelInteractiveTransition()
                }
            }
        }
    }
}

所以我假设您已按照问题中提到的博客 post 中的概述为交互式过渡设置了所有其他内容,但如果您有任何其他问题或需要更多代码,请不要犹豫在我的回答中发表评论。