简体   繁体   English

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

[英]Presenting view controller with custom animation using only code (No Segue)

Here's what I am tring to do: 这是我想做的事情:

Implement a side nav that will slide in with custom animation from left to right 实施侧面导航,该导航将与自定义动画一起从左向右滑动

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

But this and all other tutorials that implement this functionality use segues to present the view. 但是,本教程以及实现此功能的所有其他教程都使用segues来呈现视图。

So their code goes here: 所以他们的代码在这里:

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

But this is not an option for me! 但这不是我的选择!

My entire view controller is created by code. 我的整个视图控制器是由代码创建的。 Right now it's a dummy view controller and I am just trying to get it to slide in from left to right. 现在它是一个虚拟视图控制器,我只是想让它从左向右滑入。

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)
    }
}

And I am stuck on this step of the tutorial: https://www.thorntech.com/2016/03/ios-tutorial-make-interactive-slide-menu-swift/#four 我被困在教程的这一步: https : //www.thorntech.com/2016/03/ios-tutorial-make-interactive-slide-menu-swift/#four

This is how I am trying to set the delegate to self: 这就是我试图将委托设置为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)
    }
}

This is where I was implementing my delegate (MainVC is from where the user will slide right) 这是我实现委托的地方(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
    }

}

Since my delegate was not calling, I decided to add presentationController(forPresented,presenting,source) and put a break point in there which was hitting. 由于我的代表没有打电话,我决定添加presentationController(forPresented,presenting,source)并在其中设置一个断点。 So I suspect that I am missing some part of the code that needs to go in there. 因此,我怀疑我缺少需要放入其中的部分代码。

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

But as I went through it, the more and more it started confusing me. 但是当我经历它时,它越来越使我感到困惑。

I am sure there is some small part that I am missing. 我敢肯定,我缺少一些一小部分。 Because if it works when the delegte is set in prepare for segue, then it should also work when calling present(_,animated,completion) 因为如果在委托准备中设置了委托时它可以工作,那么在调用present(_,animated,completion)时它也应该工作

Can someone point me in right direction? 有人可以指出我正确的方向吗? How do I get the custom animations to trigger and the delegate methods to call? 如何获得自定义动画来触发和委托方法来调用? Does anyone know a tutorial that teaches how to do this using just code and no storyboard? 有谁知道一个教程,该教程仅使用代码而不使用情节提要进行教学?

I referred to answer by @Womble 我提到@Womble的回答

" UIViewControllerTransitioningDelegate method not called in iOS 7 " iOS 7中未调用UIViewControllerTransitioningDelegate方法

Turns out the method that I was using was wrong and Xcode didn't warn of this. 原来我使用的方法是错误的,并且Xcode没有对此发出警告。 Kinda odd. 有点奇怪

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何自定义Modal View Controller呈现动画? - How to custom Modal View Controller presenting animation? 呈现具有透明度和动画的视图控制器 - Presenting a view controller with transparency and animation 用动画替换root-view-controller自定义segue? - Replace-root-view-controller custom segue with animation? 是否可以通过MKAnnotation传递数据而无需使用segue并仅提供View Controller? - Is it possible to pass data from MKAnnotation without using a segue and just presenting a View Controller? 使用自定义UIPresentationController子类呈现视图控制器时出现神秘崩溃 - Mysterious crash when presenting a view controller using a custom UIPresentationController subclass 呈现视图 controller 在 animation 之后消失 - Presenting View controller disappears after animation 自定义segue到同级视图控制器 - Custom segue to sibling view controller 通过导航呈现自定义尺寸的视图控制器 - Presenting a view controller of custom size with navigation 在 iPad 上展示视图控制器只允许 .Popover - Presenting View Controller on iPad only allows .Popover 仅在UISplitViewController的DetailViewController中呈现视图控制器 - Presenting a View controller only in the DetailViewController of UISplitViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM