2016-07-16 102 views
0

我設置了我拖動到故事板的視圖的約束,以便視圖覆蓋整個屏幕。 (我從頂部,左側,底部和右側固定了0)。當視圖出現時,我希望視圖縮小。該常數應該從0到20之間動畫。UIView的動畫約束(作爲主視圖的子視圖)

在應用程序中,它會更改常量,但不會爲其設置動畫效果。此外,在收縮視圖的後面,儘管它應該是白色的,但所有東西都是黑色的(因爲在該視圖後面是主視圖是白色的)。

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()。但是上面的那些約束也沒有這個功能。爲什麼?

回答

2

@try this

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