gpt4 book ai didi

ios - 如何用一个动画删除所有子 UIViewController 的动画?

转载 作者:行者123 更新时间:2023-11-29 05:25:56 24 4
gpt4 key购买 nike

我想用一个动画删除所有子 UIViewController。所以我想知道如果我在迭代所有子项时添加删除动画,它会正确显示吗?我想要实现的效果只是用于此删除的一个动画。

func removeAllChildrenWithAnimation() {
if let children = viewController?.children {
for child in children {
child.willMove(toParent: nil)
UIView.animate(withDuration: 0.25, animations: {
child.view.alpha = 0.0
}) { (_) in
child.view.removeFromSuperview()
child.removeFromParent()
}
}
}
}

最佳答案

您应该将所有内容放在一个动画 block 中:

我无法测试这段代码 - 我什至无法在 Xcode 中输入它并对其进行语法检查或编译 - 所以如果它包含一两个拼写错误,请原谅我。但总的来说,这就是它应该如何工作......

func removeAllChildrenWithAnimation() {
guard let children = viewController?.children else { return }

children.forEach { $0.willMove(toParent: nil) }

UIView.animate(withDuration: 0.25, animations: {
children.forEach { $0.viewIfLoaded?.alpha = 0.0 }
}, completion: { finished in
if finished {
children.forEach {
$0.viewIfLoaded?.removeFromSuperview()
$0.removeFromParent()
}
}
})
}

关于ios - 如何用一个动画删除所有子 UIViewController 的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58171264/

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