【问题标题】:Animate hidden property on UILabels in UIStackView causes different animationsUIStackView中UILabels上的动画隐藏属性导致不同的动画
【发布时间】:2016-03-06 14:52:38
【问题描述】:

UIStackView 使得使用 UIView 的 hidden 属性创建漂亮的动画变得非常容易。我有两个UIStackViews,每个UILabelsarrangedSubviews 中,当我将一个新的UILabel 添加到UIStackView 时,它应该在正确的索引处显示标签的动画,推动标签上面和下面。

这个效果用UIStackViews很容易做到:

descriptionLabel.hidden = true 

let count = descriptionStack.arrangedSubviews.count
descriptionStack.insertArrangedSubview(expenseLabel.descriptionLabel, atIndex: count - 1)

UIView.animateWithDuration(0.5) {
  descriptionLabel.hidden = false
}

我想同时为两个不同的UIStackViews 执行此效果,但这会导致一些奇怪的行为,其中一个动画正确,而另一个从视图顶部下降。 假设上面的代码可以为其他一些视图重复并创建相同的动画:

descriptionLabel.hidden = true 
costLabel.hidden = true

let count = descriptionStack.arrangedSubviews.count
descriptionStack.insertArrangedSubview(expenseLabel.descriptionLabel, atIndex: count - 1)
costStack.insertArrangedSubview(expenseLabel.costLabel, atIndex: count - 1)

UIView.animateWithDuration(0.5) {
  descriptionLabel.hidden = false
  UIView.animateWithDuration(0.5) {
    costLabel.hidden = false
  }
}

在此示例中,costLabel 的动画效果正确,而descriptionLabelUIStackView 的顶部下降。颠倒顺序会导致costLabel 插入,descriptionLabel 正确设置动画。

我已经尝试过这个动画代码的变体,例如不嵌套动画并使用UIView.animateKeyframesWithDuration

如下所示,costLabel 插入,descriptionLabel 正确设置动画:

UIView.animateWithDuration(0.5) {
  descriptionLabel.hidden = false
}

UIView.animateWithDuration(0.5) {
  costLabel.hidden = false
}

我无法弄清楚为什么动画总是彼此不同。如何同时为两个标签设置动画,并在它们出现在正确索引处产生效果,将标签推到上方和下方?

【问题讨论】:

    标签: ios animation uistackview animatewithduration


    【解决方案1】:

    我也有同样的问题。我发现设置UILabelContent Mode 属性似乎改变了UIView 动画的执行方式。就我而言,我想实现从上到下的动画。默认动画是从左侧滑入并调整大小。将内容模式设置为 Top 对我有用。

    也许有帮助。

    【讨论】:

    • 更改Content Mode 肯定会影响UILabel 的动画效果,但它似乎不太一致。将其设置为.Left 会使标签“下拉”,但将其设置为.Right 会使它从视图的左侧出现。至少从侧面看起来更好看,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 2018-04-23
    • 1970-01-01
    相关资源
    最近更新 更多