如何实现带有过渡效果的removeFromSuperview()?

3
我使用以下代码将一个视图添加为子视图:

我使用以下代码将一个视图添加为子视图:

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


})

但是这并不会导致平滑的转换。我该如何使用平滑的转换方式删除已添加的子视图,类似于它展示的方式?

要做到这一点,您只需要更改动画方法中的框架,使得您的视图向右远离屏幕,然后在完成处理程序中,一旦动画完成,调用removeFromSuperview()方法。 - Rishabh
1个回答

1

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 {$0}
            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 {$0}
            NSLayoutConstraint.activate(constrs)

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

这是我用来实现在容器中推送和弹出视图控制器的一种方法,但动画是相同的,只需更改约束条件,以保持侧边视图控制器的窄。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接