仅使用代码(无 Segue)呈现带有自定义动画的视图控制器



这是我想要做的:

实现一个侧导航,该导航将从左到右滑动带有自定义动画

我指的是这个:https://www.thorntech.com/2016/03/ios-tutorial-make-interactive-slide-menu-swift/

但是,本教程和实现此功能的所有其他教程都使用 segue 来呈现视图。

所以他们的代码在这里:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if let destinationViewController = segue.destinationViewController as? MenuViewController {
destinationViewController.transitioningDelegate = self
}
} 

但这对我来说不是一个选择!

我的整个视图控制器都是由代码创建的。现在它是一个虚拟的视图控制器,我只是想让它从左到右滑入。

import UIKit
class SideNavVC: UIViewController {
static let sideNav = SideNavVC()
override func viewDidLoad() {
super.viewDidLoad()
let dummyView = UIView(frame: CGRect(x: 10, y: 200, width: 100, height: 100))
dummyView.backgroundColor = accentColor
view.addSubview(dummyView)
let closeBtn = UIButton(frame: CGRect(x: 4, y: 30, width: 200, height: 20))
closeBtn.addTarget(self, action: #selector(closeViewTapped), for: .touchUpInside)
closeBtn.setTitle("Close", for: .normal)
view.addSubview(closeBtn)
view.backgroundColor = confirmGreen
}
@objc func closeViewTapped(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}

我被困在本教程的这一步:https://www.thorntech.com/2016/03/ios-tutorial-make-interactive-slide-menu-swift/#four

这就是我尝试将委托设置为自我的方式:

func addSlideGesture() {
let edgeSlide = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(presentSideNav))
edgeSlide.edges = .left
view.addGestureRecognizer(edgeSlide)
}
@objc func presentSideNav() {
if presentedViewController != SideNavVC.sideNav {
SideNavVC.sideNav.transitioningDelegate = self
SideNavVC.sideNav.modalPresentationStyle = .custom
self.present(SideNavVC.sideNav, animated: true, completion: nil)
}
}

这是我实现委托的地方(MainVC 是用户向右滑动的地方(

extension MainVC: UIViewControllerTransitioningDelegate {
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
print("Source: (source)")
print("Destination (presenting)")
if source == self && presenting == SideNavVC.sideNav {
print("PRESENTING SIDE NAV")
return RevealSideNav()
}
return nil
}
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
//Added a break point here. It is hitting.
return nil
}
}

由于我的代表没有打电话,我决定添加presentationController(forPresenting、presenting、source(并在那里放置一个中断点。所以我怀疑我错过了需要进入那里的代码的某些部分。

当我用谷歌搜索它时,我发现这个UIPresentationController教程 https://www.raywenderlich.com/915-uipresentationcontroller-tutorial-getting-started

但随着我经历它,它越来越让我感到困惑。

我确信我错过了一些小部分。因为如果它在 delegte 设置为准备 segue 时有效,那么它在调用present(_,animated,complete( 时也应该有效

有人可以指出我正确的方向吗?如何获取要触发的自定义动画和要调用的委托方法?有谁知道一个教程,教如何只使用代码而不是故事板来做到这一点?

我指的是回答@Womble

"UI ViewControllerTransitioningDelegate 方法未在 iOS 7 中调用">

事实证明,我使用的方法错误,Xcode没有警告这一点。有点奇怪。

func animationController(forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController)
-> UIViewControllerAnimatedTransitioning?
{
print("Source: (source)")
print("Destination (presenting)")
// ... return your cached controller object here.
return RevealSideNav()
}

最新更新