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

时间:2018-09-28 20:15:31

标签: ios swift segue core-animation

这就是我要做的事情:

实施侧面导航,该导航将以自定义动画从左向右滑入

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

但是,此实现以及所有其他实现此功能的教程都使用segues来显示视图。

所以他们的代码在这里:

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

这就是我试图将委托设置为self的方式:

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(forPresented,presenting,source)并在其中设置一个中断点。因此,我怀疑我缺少需要放入其中的部分代码。

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

但是当我经历它时,它越来越使我感到困惑。

我确定我缺少一些小部分。因为如果在将委托设置为准备好时将其设置为工作,则在调用 present(_,animated,completion)

时也应工作

有人可以指出我正确的方向吗?如何获得自定义动画来触发和委托方法来调用?有谁知道一个教程,该教程仅使用代码而不使用情节提要来教授如何做到这一点?

1 个答案:

答案 0 :(得分:0)

我指的是@Womble的答案

UIViewControllerTransitioningDelegate method not called in 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()
    }