如何使用转换删除FromSuperview()

时间:2017-08-28 09:56:44

标签: ios swift addsubview catransition

我使用以下代码

添加了一个视图作为子视图
    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)



    })

结果是视图控制器(侧面菜单)从左侧滑动并显露出来。 我想从右到左删除添加的子视图 我尝试使用以下代码从动画中删除superview

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


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


})

但这不会导致平滑过渡。如何通过平滑过渡删除添加的子视图,类似于显示的方式???

1 个答案:

答案 0 :(得分:1)

removeFromSuperview()方法不可动画,你可以做的是让alpha变为0,完成后你可以安全地从超级视图中删除。
如果你想获得与push相同的效果,你只需要获取代码并创建相反的过渡。由于您似乎正在使用视图控制器,因此可以利用过渡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()
            }
        }
    }

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