美文网首页
UIView的layout与动画

UIView的layout与动画

作者: 我是花老虎 | 来源:发表于2016-07-13 00:17 被阅读45次

可以做动画的属性

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
        }

相关文章

  • UIView的layout与动画

    可以做动画的属性 Changes to several view properties can be animat...

  • iOS动画

    iOS动画-从UIView动画说起iOS动画-Transform和KeyFrame动画iOS动画-layout动画...

  • 2019-11-12

    swift UIView CALayer动画的暂停与继续 开始动画 UIView.animate(with...

  • 动画案例:加入购物车

    UIView 动画 CALay 动画 UIView 动画: UIView 动画:UIView的属性动画 就是在一定...

  • iOS动画专题·UIView二维形变动画与CAAnimation

    iOS动画专题·UIView二维形变动画与CAAnimation核心动画 iOS动画专题·UIView二维形变动画...

  • iOS 动画十:Layer Keyframe Animation

    Layer 上的 Keyframe 动画与 UIView 上的关键帧动画有些不同。UIView 关键帧动画是将独立...

  • iOS 动画

    UIView基础动画实现方式一 UIView位置大小动画UIView颜色动画UIView透明度动画 UIView基...

  • UIView Layout

    简单记录 自学非博客:我们的View在Init的时候是做了以下顺序的操作的 1.约束2.layout3.displ...

  • UIView Layout

    layoutIfNeeded 强制进行layout,从当前view开始 对当前view及子view进行layout

  • 动画篇-layout动画初体验

    动画篇-layout动画初体验 动画篇-layout动画初体验

网友评论

      本文标题:UIView的layout与动画

      本文链接:https://www.haomeiwen.com/subject/yiesjttx.html