繁体   English   中英

UIView的动画约束(作为主视图的子视图)

[英]Animate constraints of a UIView (as subview of the main view)

我设置了拖到情节提要上的视图的约束,以使视图覆盖整个屏幕。 (我从顶部,左侧,底部和右侧将其固定为0)。 当视图出现时,我希望视图缩小。 该常数应从0到20进行动画处理。

在应用程序中,它会更改常量,但不会为其设置动画。 同样,在该缩小的视图后面,所有内容都是黑色,尽管它应该是白色(因为该视图后面是主视图,它是白色)。

override func viewDidAppear(animated: Bool) {

    UIView.animateWithDuration(2.5, animations: {
        self.leftPin.constant = 20.0
        self.topPin.constant = 20.0
        self.rightPin.constant = 20.0
        self.bottomPin.constant = 20.0
        // bg is my view I want to shrink
        self.bg.layoutIfNeeded()
        })
}

我读到为了将新的约束“应用”到实际布局,您必须运行layoutIfNeeded()。 但是上面的那些约束也可以在没有该功能的情况下工作。 为什么?

@尝试这个

override func viewDidAppear(animated: Bool) {

    self.leftPin.constant = 20.0
    self.topPin.constant = 20.0
    self.rightPin.constant = 20.0
    self.bottomPin.constant = 20.0

    UIView.animateWithDuration(2.5, animations: {

    self.view.layoutIfNeeded()
    })
}

暂无
暂无

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

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