Not all content is animated inside a stack view when hiding it

时间:2017-04-10 00:23:16

标签: ios swift animation uiview

I'm currently working on a iOS (swift 3) app. I have a simple vertical stack view containing 2 horizontal stack views. In some cases I want to hide the bottom one. I do so by using the following code

 UIView.animate(withDuration: 3) {
                self.bottomStackView.isHidden = true;
 };

The animation shown below doesn't really do what I would expect:

enter image description here

While the border of the buttons is animated properly when hiding, the text inside each button doesn't seem to be affected until the very end. Any idea as to how I could fix this?

2 个答案:

答案 0 :(得分:2)

我不确定这一点,我认为堆栈视图有时会导致奇怪的行为。您是否尝试在UIView.animate块中添加“self.view.layoutIfNeeded()”?像这样:

UIView.animate(withDuration: 3) {
      self.bottomStackView.isHidden = true
      self.view.layoutIfNeeded()
}

我认为如果你将“self.bottomStackView.isHidden = true”置于UIView.animate之上,它也应该有效,但不确定,不是它的专家。 另外,我认为你不需要使用“;”在你的swift代码行之后:)

答案 1 :(得分:2)

我一直在研究这个主题,似乎大多数articles都建议使用堆栈来执行动画会很好。但是我也发现动画只适用于animatable properties isHidden 不是其中之一。

在经过一些试验和错误后,我发现 isHidden 可以使用堆栈视图进行动画制作,但是你可能会发现孩子行为不端。到目前为止,我发现的唯一解决方法是这样:

        let duration = 0.5;
        let delay = 0;
        UIView.animate(withDuration: duration, delay: delay, animations: {
            self.bottomStack.isHidden = self.hideBottomStack;
        })
        UIView.animate(withDuration: duration/2, delay: delay, animations: {
            self.bottomStack.alpha = 0;
        })

你在这里注意到我基本上"转向#34; alpha属性在我隐藏堆栈的一半时间内降至0。这样可以在文本与上层堆栈重叠之前隐藏文本。还要注意我也可以决定做这样的事情:

       UIView.animate(withDuration: duration, delay: delay, animations: {
           self.bottomStack.alpha = 0;
       }, completion: { (_) in 
           self.bottomStack.isHidden = true;
       })

这也会隐藏底部堆栈,但是你会失去隐藏动作以支持淡入淡出的动作,并在淡入淡出完成后隐藏堆栈。