如何使用过渡移除 FromSuperview()

How to removeFromSuperview() with a transition

我使用以下代码添加了一个视图作为子视图

    let controller = SideMenuViewController.instantiateViewControllerFromStoryboard(storyBoardName: "Module1") as! SideMenuViewController

    UIView.animate(withDuration:0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
    }, completion: {
        (finished: Bool) -> Void in


        controller.view.tag = 100

        let transition = CATransition()
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromLeft
        controller.view.layer.add(transition, forKey: nil)
        self.view.addSubview(controller.view)
        self.addChildViewController(controller)
        controller.didMove(toParentViewController: self)



    })

结果是一个视图控制器(侧边菜单)从左侧滑动并显示出来。 我想通过从右到左的过渡删除添加的子视图 我尝试使用以下代码从带有动画的超级视图中删除

UIView.animate(withDuration:0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: {}, completion: {


self.view.viewWithTag(100)?.removeFromSuperview()


})

但这不会导致平滑 transition.How 我可以删除添加的子视图,使其平滑过渡,类似于它显示的方式吗???

removeFromSuperview() 方法不可设置动画,您可以做的是使 alpha 变为 0,完成后您可以安全地从超级视图中移除。
如果你想获得与推送相同的效果,你只需要使用你的代码并创建相反的转换。由于您似乎正在使用视图控制器,因此您可以利用转换 API.

func push(_ viewController: UIViewController, animated: Bool, completion: (()->())?) {
        let oldVC = viewControllersStack.topItem()!
        viewController.view.frame = oldVC.view.frame
        self.addChildViewController(viewController)
        oldVC.willMove(toParentViewController: nil)
        let duration = animated ? Constants.GeneralValues.PopPushAnimationDuration : 0.0
        transition(from: oldVC, to: viewController, duration: duration, options: [], animations: { () -> Void in
            let animation = CATransition()
            animation.duration = CFTimeInterval(duration)
            animation.type = kCATransitionMoveIn
            animation.timingFunction = CAMediaTimingFunction(name: "easeInEaseOut")
            animation.subtype = "fromRight"
            animation.fillMode = "forwards"
            self.mainContainerView.layer.add(animation, forKey: "animoteKey")

            // Constraint
            guard let v = viewController.view else {
                return
            }
            v.translatesAutoresizingMaskIntoConstraints = false
            let hConstr = NSLayoutConstraint.constraints(withVisualFormat: "H:|[v]|", options:[], metrics:nil, views:["v":v])
            let vConstr = NSLayoutConstraint.constraints(withVisualFormat: "V:|[v]|", options:[], metrics:nil, views:["v":v])
            let constrs: [NSLayoutConstraint] = [hConstr, vConstr].flatMap {[=10=]}
            NSLayoutConstraint.activate(constrs)
            }) { (finished) -> Void in
            oldVC.removeFromParentViewController()
            self.viewControllersStack.push(viewController)
            viewController.didMove(toParentViewController: self)
            if let completion = completion {
                completion()
            }
        }
    }

    func popViewController(_ animated: Bool, completion: (()->())?) {
        let oldVC = viewControllersStack.topItem()!
        let viewController = viewControllersStack.penultimate!
        viewController.view.frame = oldVC.view.frame
        self.addChildViewController(viewController)
        oldVC.willMove(toParentViewController: nil)
        let duration = animated ? Constants.GeneralValues.PopPushAnimationDuration : 0.0
        transition(from: oldVC, to: viewController, duration: duration, options: [], animations: { () -> Void in
            let animation = CATransition()
            animation.duration = CFTimeInterval(duration)
            animation.type = kCATransitionReveal
            animation.timingFunction = CAMediaTimingFunction(name: "easeInEaseOut")
            animation.subtype = "fromLeft"
            animation.fillMode = "forwards"
            self.mainContainerView.layer.add(animation, forKey: "animoteKey")
            // Constraint
            guard let v = viewController.view else {
                return
            }
            v.translatesAutoresizingMaskIntoConstraints = false
            let hConstr = NSLayoutConstraint.constraints(withVisualFormat: "H:|[v]|", options:[], metrics:nil, views:["v":v])
            let vConstr = NSLayoutConstraint.constraints(withVisualFormat: "V:|[v]|", options:[], metrics:nil, views:["v":v])
            let constrs: [NSLayoutConstraint] = [hConstr, vConstr].flatMap {[=10=]}
            NSLayoutConstraint.activate(constrs)

            }) { (finished) -> Void in
            print("Fine")
            oldVC.removeFromParentViewController()
            _ = self.viewControllersStack.pop()
            viewController.didMove(toParentViewController: self)
            if let completion = completion {
                completion()
            }
        }
    }

这是我用来实现你想要的相同实现的一种实现,但用于在容器中推送和弹出视图控制器,但动画是相同的,只是更改约束,以保持侧视图控制器薄。