首页 > 代码库 > 可以做动画的属性

可以做动画的属性

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.

也就是说这些动属性的变化在动画的线程中时,就会有动画的效果。

技术分享

        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

  • setNeedsLayout

    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,

也就是说这个函数并不会是使界面马上更新,要等到下一次更新(页面刷新?)时。在动画的block中设置这个属性也不会有动画。
- layoutIfNeeded

Lays out the subviews immediately

因此调用这个函数会马上是界面更新。如果在block中调用这个函数,会有动画的效果。

技术分享

    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
        }

可以做动画的属性