繁体   English   中英

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

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

我正在为自定义 UIView 设置动画以展开,如下面的 gif 所示(红色视图)。 单击打开/关闭按钮时,我希望左侧图标垂直保持在其超级视图的中心。 如下图所示,只要我点击按钮,图标就会在动画完成后弹出到它应该出现的位置。 按钮也是如此,但它并不那么明显,因为它被暂时隐藏起来,无法点击它。

我还希望UILabel能够为其大小设置动画,而不是在您单击“关闭”时立即弹出到最小高度。

在 Storyboard 中,我将图标和按钮设置为与超级视图居中对齐,这对我来说似乎是实现我想要的正确方法。 我会假设当我为超级视图设置动画时,子视图将在动画期间保持居中,但它们会立即移动到正确的点,但在动画完成后。

gif

我的动画代码:

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
}

当超级视图动画时,我如何/如何更改以使图标和按钮保持居中?..

您需要启用Clip Subviews 这是 UIViews 默认禁用的值。

如果您通过 Interface Builder 创建视图,请转到Attributes Inspector并选中Clip Subviews复选框:

在此处输入图片说明

如果您通过代码创建视图,则使用:

view.clipsToBounds = true

暂无
暂无

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

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