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 }