简体   繁体   English

uistackview中的排列子视图中的动画“跳跃”

[英]Animation within arranged subview in uistackview “jumps”

I have a horizontal UIStackView containing several arranged subviews. 我有一个包含几个排列的子视图的水平UIStackView。

I want to remove one arranged subview (let's call it A) when a user clicks on a button, but just before that I want to do a quick animation. 我想在用户点击按钮时删除一个已安排的子视图(让我们称之为A),但就在此之前我想要做一个快速动画。 I want to do my own animation so I can't only animate stackView.removeArrangedSubview which would do a default basic animation, and not the special one I want. 我想做自己的动画,所以我不仅可以动画制作默认基本动画的stackView.removeArrangedSubview ,而不是我想要的特殊动画。

Within A, I have another view (let's call it B) that is centeredX and centeredY using Auto Layout. 在A中,我有另一个视图(我们称之为B),它使用自动布局进行居中X和居中。

When I animate the A.widthAnchor.constant of my arrangedSubview A to 0, so the others arrangedsubviews come in the place of it progressively, the children B loses just before animation its CenterX and goes to the left. 当我将被安排的子视图A的A.widthAnchor.constant设置为0时,其他arrangedsubviews的子视图逐渐到达它的位置,孩子B在动画之前就失去了它的CenterX并向左移动。 Or maybe it doesn't lose centerX, but it is centeredX on a tiny A (that has width = 0, and starts on the left), but A shouldn't have its width to 0 immediately, but after the animation completes... 或者它可能不会丢失centerX,但它在一个微小的A(宽度= 0,并从左侧开始)居中X,但A的宽度不应该立即为0,但在动画完成后... 。

I try as much as I can to explain clearly but I know it may be confusing. 我尽可能多地尝试解释清楚,但我知道这可能令人困惑。 Here is the video showing the problem. 这是显示问题的视频。 The image (B), which is in an arrangedsubview A (taking all screen) "jumps" on the left before the animation starts. 在动画开始之前,在arrangedsubview A(拍摄所有屏幕)中的图像(B)在左侧“跳跃”。

https://drive.google.com/open?id=1nTDRe5zirbuQi86WVBTuU-iDU8aq0lcw https://drive.google.com/open?id=1nTDRe5zirbuQi86WVBTuU-iDU8aq0lcw

Here is my very simple code to make this animation : 以下是制作此动画的非常简单的代码:

   UIView.animate(withDuration:4,delay:0, options:.curveEaseInOut, animations : {

        self.c.constant = 0 // c is A.widthAnchor constraint
        self.stackView.layoutIfNeeded()

    },completion: { _ in
        // done
    })

I found myself : Actually, the problem was that when I do 我发现自己:实际上,问题在于我做的时候

Stackview.layoutIfNeeded()

What is inside the stackview can animate but not the stackview itself. stackview中的内容可以动画,但不能是stackview本身。 So the stackview updated its width before the animation started. 因此stackview在动画开始之前更新了它的宽度。 I just changed to 我刚改变

superviewThatContainTheStackView.layoutIfNeeded()

So everything works now :) 所以现在一切正常:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM