簡體   English   中英

viewController我解雇時存在

[英]viewController Present when I dismissed it

我有一個viewcontroller,它與tableViewController相連,我想通過過渡來呈現它,並且從頂部作為sideMenu可以找到一些代碼,它工作正常,但是當我關閉本身呈現的ViewController時我無法理解該部分

//
//  ViewController.swift
//  ProTansition
//
//  Created by Teodik Abrami on 11/1/18.
//  Copyright © 2018 Teodik Abrami. All rights reserved.
//

import UIKit

class ViewController: UIViewController, MenuTransitionManagerDelegate {

    func dismiss() {
        dismiss(animated: true, completion: nil)
        print("dismiss run")
    }
    var menuTransition = MenuTransitionManager()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        print("ViewController Appear")
    }

    override func viewDidDisappear(_ animated: Bool) {
        print("viewcontroller disapear")
    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destinaion = segue.destination
        destinaion.transitioningDelegate = menuTransition
        menuTransition.delegate = self
    }
}

tableView是具有5行且沒有特殊代碼的普通tableview

和過渡

//
//  MenuTransitionManager.swift
//  ProTansition
//
//  Created by Teodik Abrami on 11/1/18.
//  Copyright © 2018 Teodik Abrami. All rights reserved.
//

import Foundation
import UIKit
@objc protocol MenuTransitionManagerDelegate {

    func dismiss()
}

class MenuTransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
    let duration = 2.0
    var isPresenting = false
    var delegate: MenuTransitionManagerDelegate?
    var snapShot: UIView? {
        didSet {
            if let delegate = delegate {
                let tap = UITapGestureRecognizer(target: delegate, action: #selector(delegate.dismiss))
                snapShot?.addGestureRecognizer(tap)
            }
        }
    }

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {
            return
        }

        guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
            return
        }
        let container = transitionContext.containerView
        let moveDown = CGAffineTransform.init(translationX: 0, y: container.frame.height - 150)
        if isPresenting {
            container.addSubview(toView)
            snapShot = fromView.snapshotView(afterScreenUpdates: true)
            container.addSubview(snapShot!)
        }

        UIView.animateKeyframes(withDuration: duration, delay: 0, options: [], animations: {
            if self.isPresenting {
                self.snapShot?.transform = moveDown
            } else {
                self.snapShot?.transform = CGAffineTransform.identity
            }
        }) { (finished) in
            transitionContext.completeTransition(true)
            if !self.isPresenting {
                self.snapShot?.removeFromSuperview()
            }
        }
    }

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        isPresenting = true
        return self
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        isPresenting = false
        return self
    }

}

我給快照添加了手勢,並且當它的竊聽協議有效並且在viewcontroller中關閉時,viewController出現時,我什至不理解為什么以及為什么代碼在未顯示的控制器上運行

這是我無法弄清楚為什么將其關閉時顯示的視圖控制器

發生在按下menuButton時

設置isPresenting = trueisPresenting = false整個策略注定會失敗,因為這兩段代碼都會在兩種情況下運行。 您必須通過使用兩個不同的animationController對象(而不是兩次都返回self )或查看哪個視圖控制器是from視圖控制器和哪個是to視圖控制器來區分演示與解雇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM