2015-03-25 90 views
14

在自动布局之前,我通过将框架设置为animateWithDuration以上来动画背景的高度。使用自动布局快速动画UIView高度

func setUpBackground() { 
    self.backgroundView.frame = CGRect(x: 0, y: 0, width: 320, height: 10) 
    self.backgroundView.backgroundColor = UIColorFromRGB(0x2d2d2d).CGColor 
} 

func AnimateBackgroundHeight() { 
    UIView.animateWithDuration(0.5, animations: { 
     self.backgroundView.frame = CGRect(x: 0, y: 0, width: 320, height: 600)  
    }) 
} 

我将我的项目自动布局后,我注意到,出现动画,但后台的高度弹回后,原来的大小/风格(Interface Builder中设置)。我读到,自动布局打开时,约束将覆盖UIView尺寸CGRect

因此,我想知道如何去实现与自动布局ON相同的高度变化动画效果。

回答

35

给你的backgroundView一个高度约束,并为它制作一个IBOutlet。在代码中,修改约束的常量值。

func AnimateBackgroundHeight() { 
    UIView.animateWithDuration(0.5, animations: { 
     self.heightCon.constant = 600 // heightCon is the IBOutlet to the constraint 
     self.view.layoutIfNeeded()  
    }) 
} 
+0

啊我明白了!这很好。非常感谢。 – Poyi 2015-03-25 06:06:04

+0

omg .. layoutIfNeeded()做了诡计...我错过了它 – 2017-05-24 04:22:25

5

也为那些你们谁发现这个帖子,试图找出为什么一个过渡改变一个视图的大小将块状或不洁净的,你只需要找到负责的观点,并呼吁self.view.layoutIfNeeded()UIView.animateWithDuration块内。我只是将自己的头撞在墙上,试图用各种限制来制造不同的东西,并制作相框,直到认识到self.view.layoutIfNeeded()只要您将它放置在正确的位置就可以平滑过渡。

+1

花了7个小时试图弄清楚为什么我的布局在动画制作时突破。看到你的帖子后,我意识到我正在调用layoutIfNeeded而不是superview。谢谢!! – Starlord 2017-01-07 19:57:05

相关问题