UIStackView使用UIView的hidden属性可以非常轻松地创建精美的动画。我有两个UIStackViews每个UILabels在arrangedSubviews当我添加一个新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
}
Run Code Online (Sandbox Code Playgroud)
我想同时针对两个不同的对象执行此效果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
}
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,costLabel正确设置了动画效果,而descriptionLabel从的顶部插入UIStackView。反转顺序会导致costLabel插入并descriptionLabel正确设置动画。
我尝试了这种动画代码的变体,例如不嵌套动画并使用 …