viewController我解雇时存在

时间:2018-11-01 23:42:31

标签: ios swift uiviewanimationtransition

我有一个viewcontroller,它紧靠tableViewController,我想通过过渡来呈现它,并且从顶部作为sideMenu,我发现一些代码,它工作正常,但是当我关闭它本身呈现的ViewController时,我无法理解该部分< / p>

//
//  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出现,我什至不理解为什么以及为什么代码在未显示的控制器上运行

this is the view controller that i cant figured out why it present when i dismiss it

this happend when menuButton pressed

1 个答案:

答案 0 :(得分:0)

您设置isPresenting = trueisPresenting = false的整个策略注定会失败,因为这两段代码都会在两种情况下运行。您必须通过使用两个不同的animationController对象(而不是两次都返回self)或通过查看哪个视图控制器是from视图控制器以及哪个是视图控制器来将演示与解雇区分开来。 to视图控制器。