gpt4 book ai didi

swift - 将 alpha 设置回 1.0 后,不会重新出现任何按钮/标签/任何内容

转载 作者:行者123 更新时间:2023-11-30 13:15:41 24 4
gpt4 key购买 nike

我想要的:调用 Viewcontroller 后,我想淡入此 Viewcontroller 中的所有元素(背景除外)。背景必须保留。

出了什么问题:正如您在我的代码中看到的,要淡入某些内容,首先需要将其删除。删除部分做得很好。但是,当我想让它重新出现时,什么也没有发生。当我使用 print 函数查看随机标签的 alpha 值时,它被设置为 1.0。我不知道我做错了什么。

这是我的代码:

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
Removeeverything()
Fadein()
}

func Removeeverything() {
for view in self.view.subviews as [UIView] {
if let btn = view as? UIButton {
UIView.animateWithDuration(0.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {

btn.alpha = 0.0

}, completion: nil)
}
}
for view in self.view.subviews as [UIView] {
if let btn = view as? UILabel {
UIView.animateWithDuration(0.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {

btn.alpha = 0.0

}, completion: nil)
}
}
for view in self.view.subviews as [UIView] {
if let btn = view as? UIImageView {
UIView.animateWithDuration(0.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {

btn.alpha = 0.0

}, completion: nil)
}
}
}

func Fadein() {
Backgrond.alpha = 1.0
for view in self.view.subviews as [UIView] {
if let btn = view as? UIButton {
UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {

btn.alpha = 1.0

}, completion: nil)
}
}
for view in self.view.subviews as [UIView] {
if let btn = view as? UILabel {
UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {

btn.alpha = 1.0

}, completion: nil)
}
}
for view in self.view.subviews as [UIView] {
if let btn = view as? UIImageView {
UIView.animateWithDuration(0.7, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {

btn.alpha = 1.0

}, completion: nil)
}
}

}

最佳答案

我想说问题是 UIView.animateWithDuration 是异步执行的(也就是说 - 你的 fadeIn() 函数将在 中的动画之前执行removeEverything() 已完成)。

只需从 Removeeverything() 中删除 UIView.animateWithDuration 操作即可。由于您已将持续时间参数设置为 0.0,因此不会真正改变任何内容(除了代码现在应该可以工作)

func removeEverything() {
for v in view.subviews.filter({ $0 is UIButton || $0 is UILabel || $0 is UIImageView }) {
v.alpha = 0
}
}

func fadeIn() {
// Background.alpha = 1.0
UIView.animateWithDuration(1.0, delay: 0, options: .CurveEaseIn, animations: {
for v in view.subviews.filter({ $0 is UIButton || $0 is UILabel }) {
v.alpha = 1
}
}, completion: nil)
UIView.animateWithDuration(0.7, delay: 0, options: .CurveEaseIn, animations: {
for v in view.subviews.filter({ $0 is UIImageView }) {
v.alpha = 1
}
}, completion: nil)
}

该代码片段应该按预期工作。我在数组上使用了 filter 函数,并删除了一些不必要的转换,以使代码更具可读性。希望对您有帮助!

关于swift - 将 alpha 设置回 1.0 后,不会重新出现任何按钮/标签/任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38256853/

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