Changes to several view properties can be animated—that is, changing the property creates an animation that conveys the change to the user over a short period of time. The UIView class does most of the work of performing the actual animations but you must still indicate which property changes you want to be animated. html
也就是说这些动属性的变化在动画的线程中时,就会有动画的效果。ide
UIView.animateWithDuration(0.5) { if Holder.animated { self.myButton?.bounds = CGRectMake(0, 0, s_ScreenWidth, 30); Holder.animated = false } else { self.myButton?.bounds = CGRectMake(0, 0, s_ScreenWidth/2, 60); Holder.animated = true } }
对于另外一些属性,即便在动画的block中设置,也不会有动画效果。函数
UIView.animateWithDuration(0.5) { if Holder.animated { self.myButton?.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10) Holder.animated = false } else { self.myButton?.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10) Holder.animated = true } }setNeedsLayout与layoutIfNeeded
-
setNeedsLayoutpost
Invalidates the current layout of the receiver and triggers a layout update during the next update cycle.动画
this method makes a note of the request and returns immediately. Because this method does not force an immediate update,this
也就是说这个函数并不会是使界面立刻更新,要等到下一次更新(页面刷新?)时。在动画的block中设置这个属性也不会有动画。
- layoutIfNeededspa
Lays out the subviews immediately线程
所以调用这个函数会立刻是界面更新。若是在block中调用这个函数,会有动画的效果。3d
UIView.animateWithDuration(0.5) { if Holder.animated { self.myButton?.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10) self.myButton?.setNeedsLayout() } else { self.myButton?.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10) self.myButton?.layoutIfNeeded() Holder.animated = true }