如何在Swift中使用UIViewControllerAnimatedTransitioning而不使用Storyboard / Unwindng Segues?

时间:2015-01-22 23:16:20

标签: ios xcode swift uiviewanimationtransition

我在 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照片关闭/关闭视图控制器的方式,我知道Facebook使用滚动视图,但我需要使用UICollectionView和UIViewController充当单元格的详细视图。

1 个答案:

答案 0 :(得分:1)

所以你需要做的是将你的代码放在你的pan手势识别器的事件处理程序中。当手势识别器的状态更改为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()
                }
            }
        }
    }
}

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