我在 UIStackView 中嵌入了两个 UILabel。顶部标签始终可见,但底部标签可通过hidden
财产。我希望这个效果是动画的,所以我把它放在一个动画块中:
private func toggleResultLabel(value:Double) {
if value == 0 {
UIView.animateWithDuration(0.25) { () -> Void in
self.resultLabel.hidden = true
}
} else {
UIView.animateWithDuration(0.25) { () -> Void in
// Something weird is happening. I had to add 3 of the same statements to get
// the hidden flag to be false
self.resultLabel.hidden = false
self.resultLabel.hidden = false
self.resultLabel.hidden = false
}
}
}
问题是隐藏属性不会改变,除非我一遍又一遍地重复该语句(在本例中为 3 次)。我在闯入动画闭包时发现了这一点,并发现该属性不会更改为它的分配。现在我注意到同样的问题似乎再次随机发生。第二个标签的默认值为true
,如果相关的话。
我这里缺少什么吗,或者这是一个错误?
Update:
对于它的价值,我通过添加来让它工作removeArrangedSubview()
and addArrangedSubview()
:
if value == 0 {
UIView.animateWithDuration(0.25) { () -> Void in
self.resultLabel.hidden = true
self.heroStackView.removeArrangedSubview(self.resultLabel)
}
} else {
UIView.animateWithDuration(0.25) { () -> Void in
self.heroStackView.addArrangedSubview(self.resultLabel)
self.resultLabel.hidden = false
}
}
在 iOS 11 及更早版本中,隐藏arrangedSubview
of a UIStackView
多次使用UIView动画API,隐藏属性值“堆栈”,并且需要将隐藏设置为false
在值实际改变之前多次。
在工作中,我们决定使用 UIView 扩展和一种解决方法,该方法针对给定值仅设置隐藏一次。
extension UIView {
// Workaround for the UIStackView bug where setting hidden to true with animation
// mulptiple times requires setting hidden to false multiple times to show the view.
public func workaround_nonRepeatingSetHidden(hidden: Bool) {
if self.hidden != hidden {
self.hidden = hidden
}
}
}
这绝对是 UIKit 中的一个错误,请查看示例项目 https://github.com/nkukushkin/StackView-Hiding-With-Animation-Bug-Example清晰地再现了它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)