【问题标题】:Animate constraints of a UIView (as subview of the main view)UIView 的动画约束(作为主视图的子视图)
【发布时间】:2016-07-16 09:17:45
【问题描述】:

我设置了拖到情节提要上的视图的约束,以便视图覆盖整个屏幕。 (我从顶部、左侧、底部和右侧将其固定为 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()。但是,如果没有该功能,上面的那些约束也可以工作。为什么?

【问题讨论】:

    标签: ios swift animation autolayout


    【解决方案1】:

    @试试这个

    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()
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多