Ios 如何在Swift中使用UIViewControlleranimated转换而不使用故事板/展开序列?

Ios 如何在Swift中使用UIViewControlleranimated转换而不使用故事板/展开序列?,ios,xcode,swift,uiviewanimationtransition,Ios,Xcode,Swift,Uiviewanimationtransition,在Swift中创建自定义视图控制器转换时,我遇到了一些问题。我已经找到了这个优秀的教程,但是我很难弄清楚如何在没有故事板的情况下创建转换(我正在使用的应用程序不使用interface builder或故事板) 所需结果的GIF: 具体来说,我遇到的问题是在使用segues的这一部分: override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { let menu = segue.destinat

Swift中创建自定义视图控制器转换时,我遇到了一些问题。我已经找到了这个优秀的教程,但是我很难弄清楚如何在没有故事板的情况下创建转换(我正在使用的应用程序不使用interface builder或故事板)


所需结果的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).

因此,您需要做的是将代码放入平移手势识别器的事件处理程序中。当手势识别器的状态更改为UIGestureRecognitizerStateStarted时,需要使用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()
                }
            }
        }
    }
}

因此,我假设您已经按照问题中提到的博客文章中所述,为交互式转换设置了所有其他设置,但是如果您还有任何问题或需要更多代码,请毫不犹豫地在我的回答中进行评论。

如果您没有使用segue,您只需实例化控制器,然后在代码中推送或呈现它。这基本上就是segue所做的。现在将这个想法整合进来,会让你知道结果如何,乍一看答案是这样的。Hector,可能需要更多的帮助。我尝试将您的解决方案与教程代码结合起来,但出现了大量错误。