gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:12:03 25 4
gpt4 key购买 nike

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


期望结果的 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 详细 View 以关闭(类似于 Facebook 照片关闭它/关闭 View Controller 的方式,我知道 Facebook 为此使用 ScrollView ,但我需要使用 UICollectionView 和充当单元格详细 View 的 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()
}
}
}
}
}

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

关于ios - 如何在没有 Storyboards/Unwindng Segues 的情况下在 Swift 中使用 UIViewControllerAnimatedTransitioning?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28100429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com