将 alpha 设置回 1.0 后不会重新出现 buttons/labels/anything

No buttons/labels/anything will re-appear after I set alpha back to 1.0

我想要的:调用 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)
        }
    }

}

想一想,您是否尝试过将 alpha 设置为无法识别的低得无法识别的值,例如 0.01 而不是完全删除它们?您也可以尝试将按钮的颜色与背景相匹配,然后以这种方式将其淡化。

您可能还需要致电 self.background.alphaview.background.alpha

此外,您真的应该使用驼峰式大小写函数名称。

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

只需从您的 Removeeverything() 中删除 UIView.animateWithDuration 操作。由于您已将 duration 参数设置为 0.0,因此什么都不会真正改变(除了代码现在应该可以工作)

func removeEverything() {
    for v in view.subviews.filter({ [=10=] is UIButton || [=10=] is UILabel || [=10=] 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({ [=10=] is UIButton || [=10=] is UILabel }) {
            v.alpha = 1
        }
    }, completion: nil)
    UIView.animateWithDuration(0.7, delay: 0, options: .CurveEaseIn, animations: {
        for v in view.subviews.filter({ [=10=] is UIImageView }) {
            v.alpha = 1
        }
    }, completion: nil)
}

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