简体   繁体   English

动画 UIView 的子视图在动画期间不保持其约束

[英]Subviews of animated UIView not keeping their constraints during animation

I'm animating a custom UIView to expand, as shown in the gif below (the red view).我正在为自定义 UIView 设置动画以展开,如下面的 gif 所示(红色视图)。 When clicking the open/close-button, I want the left icon to stay in its superview's center, vertically.单击打开/关闭按钮时,我希望左侧图标垂直保持在其超级视图的中心。 As shown below, as soon as I click the button, the icon will pop to where it should be after the animation is done.如下图所示,只要我点击按钮,图标就会在动画完成后弹出到它应该出现的位置。 The same goes for the button, but it's not as noticeable as it gets temporarily hidden from clicking it.按钮也是如此,但它并不那么明显,因为它被暂时隐藏起来,无法点击它。

I was also hoping for the UILabel in between to animate its size, rather than popping to minimum height as soon as you click 'Close'.我还希望UILabel能够为其大小设置动画,而不是在您单击“关闭”时立即弹出到最小高度。

In Storyboard I have set the icon and button to align center with the superview, which to me seems to be the correct way to achieve what I want.在 Storyboard 中,我将图标和按钮设置为与超级视图居中对齐,这对我来说似乎是实现我想要的正确方法。 I would assume that when I animate the superview, that the subviews would stay center during the animation, but instead they instantly move to the point that WILL be correct, but after the animation is completed.我会假设当我为超级视图设置动画时,子视图将在动画期间保持居中,但它们会立即移动到正确的点,但在动画完成后。

gif

My animation code:我的动画代码:

UIView.animateWithDuration(0.4, animations: { () -> Void in

    var rect = self.frame; //The current frame, to change
    let oldHeight = self.frame.size.height as CGFloat

    let newSize = self.sizeForBanner() //Get the CGSize for the big banner.

    if(self.isBig) //Animate to big size
    {
        //Put the new height and origin for the large version of the view
        rect.size.height = newSize.height;
        rect.origin.y -= (rect.size.height - oldHeight)

    }else{ //Animate to small size

        //Put the new height and origin for the small version of the view

        rect.size.height = self.minimalHeight;
        rect.origin.y += oldHeight-rect.size.height
    }

        //Set the new variables
        self.frame = rect;

    }) { (Bool) -> Void in
        //Completion
}

How /what do I change to make the icon and the button to stay centered when the superview animates?..当超级视图动画时,我如何/如何更改以使图标和按钮保持居中?..

You need to have Clip Subviews enabled.您需要启用Clip Subviews This is a value that is disabled by default for UIViews.这是 UIViews 默认禁用的值。

If you created the view through Interface Builder go to Attributes Inspector and check the Clip Subviews checkbox:如果您通过 Interface Builder 创建视图,请转到Attributes Inspector并选中Clip Subviews复选框:

在此处输入图片说明

If you created the view through code then use:如果您通过代码创建视图,则使用:

view.clipsToBounds = true

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

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