UIView的layout与动画

可以做动画的属性

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
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 14,599评论 5 6
  • “我养你”是世上最动听的情话。 但它仅仅是一句情话。 说这话的人连自己都不知道。 说这话的人在说这句情话的时候,是...
    郭倍贝贝阅读 4,042评论 0 0
  • 不管是被迫写,还是喜欢写,不管是大家还是小咖,每个人在写作路上,都无一例外遇到下面三个问题,今天我主要从实际情况入...
    凤歌儿阅读 2,778评论 0 8
  • 在JSP中提供了四种属性的保存范围,所谓的属性保存范围,指的就是一个设置的对象,可以在多少个页面中保存并可以继续使...
    kylelin阅读 2,994评论 0 1
  • 1.网页的三要素: HTML:网页的结构 CSS:网页的样式 JavaScript:网页的行为 2.网页的基本结构...
    XLsn0w阅读 1,054评论 0 1

友情链接更多精彩内容