【问题标题】:UIView.animate not animating bool value perfectlyUIView.animate 没有完美地动画布尔值
【发布时间】:2016-11-17 08:50:14
【问题描述】:

我尝试为 isHidden 设置动画,它似乎工作正常,但如果我通过将 yes 设置为 true 5 次错误地为 isHidden=false 设置了 5 次动画,那么有时我应该为 isHidden 设置动画=true 2 或更多时间让我的 UIView 可见!

我错过了什么吗?

if (yes)
{
    UIView.animate(withDuration: 0.3, delay:0, animations: {
                myLabel.isHidden=false
            }
}
else
{
    UIView.animate(withDuration: 0.3, delay:0, animations: {
                myLabel.isHidden=true
            }
}

【问题讨论】:

    标签: ios animation uiview swift3


    【解决方案1】:

    您不应为视图的“isHidden”参数设置动画。您应该为其 alpha 设置动画。

    if (yes)
    {
        UIView.animate(withDuration: 0.3, delay:0, animations: {
                    myLabel.alpha=1.0
                }
    }
    else
    {
        UIView.animate(withDuration: 0.3, delay:0, animations: {
                    myLabel.alpha=0.0
                }
    }
    

    -- 更新--

    如果你想在动画之后隐藏视图,你可以使用这个:

    myLabel.isHidden=false
    UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveEaseOut, animations: {
        myLabel.alpha=1.0
      }, completion: { finished in
    
      })
    
    UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveEaseOut, animations: {
        myLabel.alpha=0.0
      }, completion: { finished in
        myLabel.isHidden=true
      })
    

    【讨论】:

    • 但我需要为 isHidden 设置动画,因此在将其从视图中移除后,自动布局会缩小布局。
    【解决方案2】:

    我认为问题在于您在 Bool 类型上使用线性动画,它只有 2 个值(false = 0,true = 1)和介于两者之间的任何其他值(它是脉冲)。

    试试这个:

    if (yes)
    {
        myLabel.alpha = 0
        myLabel.isHidden = false
        UIView.animate(withDuration: 0.3, animations: {
                    myLabel.alpha = 1
                })
    }
    else
    {
        UIView.animate(withDuration: 0.3, animations: {
                    myLabel.alpha = 0
                }, completion: { (status) in
                    myLabel.isHidden = true
                })
    }
    

    【讨论】:

    • 但我需要为 isHidden 设置动画,因此在将其从视图中移除后,自动布局会缩小布局。
    • 所以尝试使用 alpha 属性以及UILabel 框架的高度进行动画处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2012-10-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 2022-08-20
    • 2012-11-11
    相关资源
    最近更新 更多