UIView 显示/隐藏动画:隐藏动画似乎不起作用

时间:2021-01-20 07:07:48

标签: ios swift animation uiview

我正在尝试实现 UIView 动画效果,其中使用动画选项触发目标 UIView 可见,并且完成后,它返回到之前的隐藏状态。

下面是我写的代码,不过好像有问题。因为显示动画效果非常好,但是当涉及到隐藏时,UIView 会立即消失,而不是有意隐藏动画效果。

我试图改变 optionshideConsentView() 参数,但它没有改变任何东西。

我在这里遗漏了什么吗?

var consentStatusView = UIView() 

func showConsentStatusView() {
    UIView.transition(with: consentStatusView, duration: 1.0, options: .transitionCrossDissolve, animations: {
        self.consentStatusView.isHidden = false
    }, completion: { (finished) in
        self.hideConsentStatusView()
    })
}

func hideConsentStatusView() {
    UIView.transition(with: consentStatusView, duration: 1.0, options: .transitionCrossDissolve, animations: {
        self.consentStatusView.isHidden = true
    }, completion: nil)
}

1 个答案:

答案 0 :(得分:1)

您可以简单地使用以下功能:

UIView.animate(withDuration: 0.3, animations: {
   self.consentStatusView.alpha = 0 //If you show a view alpha = 1
}) { (finished) in
   self.consentStatusView.isHidden = true //If you unHide your view isHidden = false
}