我有以下动画,但似乎无法弄清楚为什么它不能始终如一地工作。堆栈视图是要在其中进行动画处理的一系列UILabel子视图:隐藏所有不带文本的标签,并显示所有这些标签。我尝试在整个过程中使用UIView.beginAnimations("Animation", nil)
和UIView.commit()
(当我将其结构化为多个动画块时)。我曾尝试将它们放在单独的块中,但似乎无法弄清楚为什么它不起作用。
在动画块中打印时显示:
<UILabel: 0x7fa237408b90; frame = (0 0; 195 130); text = '1'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x6000004645a0>>
<UILabel: 0x7fa2374088a0; frame = (195 65; 0 0); text = '0'; hidden = YES; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x600000464460>>
hide <UILabel: 0x7fa2374085b0; frame = (195 65; 0 0); text = ''; hidden = YES; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x600000464190>>
hide <UILabel: 0x7fa2374082c0; frame = (195 65; 0 0); text = ''; hidden = YES; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x600000464050>>
hide <UILabel: 0x7fa237407e50; frame = (195 65; 0 0); text = ''; hidden = YES; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x600000459270>>
hide <UILabel: 0x7fa237407b60; frame = (195 65; 0 0); text = ''; hidden = YES; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60000045a990>>
我也尝试过在动画块内部和外部的主线程上执行此操作。在块内取消动画,而在块外没有区别。另外,如果您注释掉view.isHidden = false
,则使视图未隐藏的部分似乎可以正常工作。
UIView.animate(withDuration: 0.2){[unowned self] in
self.stackView.arrangedSubviews.forEach{(view) in
if((view as! UILabel).text == ""){
print("hide \(view)")
view.isHidden = true
}else{
print(view)
view.isHidden = false
}
}
}//End of animation
非常感谢您的帮助。