gpt4 book ai didi

ios - 如何使用过渡移除 FromSuperview()

转载 作者:行者123 更新时间:2023-11-28 06:13:02 25 4
gpt4 key购买 nike

我使用以下代码添加了一个 View 作为 subview

    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)



})

结果是一个 View Controller (侧边菜单)从左侧滑动并显示出来。我想通过从右到左的过渡删除添加的 subview 我尝试使用以下代码从带动画的 super View 中删除

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


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


})

但这不会导致平滑过渡。如何以平滑过渡删除添加的 subview ,类似于它显示的方式???

最佳答案

removeFromSuperview() 方法不可设置动画,您可以做的是使 alpha 变为 0,完成后您可以安全地从 super View 中移除。
如果你想获得与推送相同的效果,你只需要使用你的代码并创建相反的转换。由于您似乎正在使用 View Controller ,因此您可以利用转换 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()
}
}
}

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

关于ios - 如何使用过渡移除 FromSuperview(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45916318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com