我创建了一个带有暗视图的幻灯片菜单。当用户在侧边菜单外点击时,我希望幻灯片菜单和变暗视图消失。我怎样才能做到这一点呢?我已经在每个类中创建了一个TapRecognizer,它可以工作。。但只对每一节课同时进行。Here是我的菜单控件,下面是我的幻灯片转换,视图变暗。现在,当我在幻灯片菜单外单击时,它将删除变暗视图。但它也应该删除幻灯片菜单。

class SlideinTransition: NSObject, UIViewControllerAnimatedTransitioning {

let menuViewController = MenuViewController()

var isPresenting = true
let dimmingView = UIView()

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

@objc func touchWasDetected() {
    print("Touch detected")
    dimmingView.removeFromSuperview()
    menuViewController.dismiss(animated: true, completion: nil)
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    guard let toViewController = transitionContext.viewController(forKey: .to),
    let fromViewController = transitionContext.viewController(forKey: .from) else { return }
    let containerView = transitionContext.containerView

    let finalWidth = toViewController.view.bounds.width * 0.3
    let finalHeight = toViewController.view.bounds.height

    if isPresenting{

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(touchWasDetected))
        dimmingView.addGestureRecognizer(tapGesture)

        //adds the dimming view
        dimmingView.backgroundColor = .black
        dimmingView.alpha = 0.0
        containerView.addSubview(dimmingView)
        dimmingView.frame = containerView.bounds
        //adds the menu view controller to our container
        containerView.addSubview(toViewController.view)

        //init frame off the screen
        toViewController.view.frame = CGRect(x: -finalWidth, y: 0, width: finalWidth, height: finalHeight)
    }

    let transform = {
        self.dimmingView.alpha = 0.5
        toViewController.view.transform = CGAffineTransform(translationX: finalWidth, y: 0)
    }

    let identity = {
        self.dimmingView.alpha = 0.0
        fromViewController.view.transform = .identity
    }

    //animates the transition
    let duration = transitionDuration(using: transitionContext)
    let isCancelled = transitionContext.transitionWasCancelled
    UIView.animate(withDuration: duration, animations: {
        self.isPresenting ? transform() : identity()
    }) { (_) in
        transitionContext.completeTransition(!isCancelled)
    }
}

}
当用户在侧菜单外/在调光视图上点击时,应同时删除侧菜单和调光视图

最佳答案

我猜你现在用的是SlideinTransition,然后解雇。
所以你应该把dimmingView.removeFromSuperview()touchWasDetected()移到UIView.animate完成。
升级版:
我已经在示例项目中运行了这个,运行良好。
初始控制器:

class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
    let transition = SlideinTransition()

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

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        transition.isPresenting = true
        return transition
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        transition.isPresenting = false
        return transition
    }
}

修改的转换:
extension UIViewController {
    @IBAction func dismissControllerAnimated() {
        dismiss(animated: true)
    }
}

class SlideinTransition: NSObject, UIViewControllerAnimatedTransitioning {
    var isPresenting = true
    let dimmingView = UIView()

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

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        guard let toViewController = transitionContext.viewController(forKey: .to),
              let fromViewController = transitionContext.viewController(forKey: .from) else { return }
        let containerView = transitionContext.containerView

        let finalWidth = toViewController.view.bounds.width * 0.3
        let finalHeight = toViewController.view.bounds.height

        if isPresenting {
            let tapGesture = UITapGestureRecognizer(target: toViewController, action: #selector(UIViewController.dismissControllerAnimated))
            dimmingView.addGestureRecognizer(tapGesture)

            //adds the dimming view
            dimmingView.backgroundColor = .black
            dimmingView.alpha = 0.0
            containerView.addSubview(dimmingView)
            dimmingView.frame = containerView.bounds
            //adds the menu view controller to our container
            containerView.addSubview(toViewController.view)

            //init frame off the screen
            toViewController.view.frame = CGRect(x: -finalWidth, y: 0, width: finalWidth, height: finalHeight)
        }

        let transform = {
            self.dimmingView.alpha = 0.5
            toViewController.view.transform = CGAffineTransform(translationX: finalWidth, y: 0)
        }

        let identity = {
            self.dimmingView.alpha = 0.0
            fromViewController.view.transform = .identity
        }

        //animates the transition
        let duration = transitionDuration(using: transitionContext)
        let isCancelled = transitionContext.transitionWasCancelled
        UIView.animate(withDuration: duration, animations: {
            self.isPresenting ? transform() : identity()
        }) { (_) in
            transitionContext.completeTransition(!isCancelled)
            if !self.isPresenting {
                self.dimmingView.removeFromSuperview()
            }
        }
    }
}

关于swift - 如何立即关闭DimmingView和SlideMenu?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56595441/

10-10 22:39