V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
xi_lin
V2EX  ›  iDev

Masonary 更新约束时怎么能带上动画效果呢?

  •  
  •   xi_lin · 2015-05-11 12:07:46 +08:00 · 4736 次点击
    这是一个创建于 3290 天前的主题,其中的信息可能已经有所发展或是发生改变。

    根据文档在updateConstraints里更换了约束

    - (void)updateConstraints {
    if (self.isEditing) {
    [self.selectedBtn mas_updateConstraints:^(MASConstraintMaker *make) {
    make.width.mas_equalTo(20);
    }];
    } else {
    [self.selectedBtn mas_updateConstraints:^(MASConstraintMaker *make) {
    make.width.mas_equalTo(0);
    }];
    }
    [super updateConstraints];
    }

    在外层调用了[view setNeedsUpdateConstraints];

    结果直接就显示出来了,没有动画效果。
    尝试了用animateWithDurtaion

    [UIView animateWithDuration:5
    animations:^{
    [view setNeedsUpdateConstraints];
    }];

    也是不行。

    应该怎么做呢?

    4 条回复    2015-05-11 21:25:06 +08:00
    vincentxue
        1
    vincentxue  
       2015-05-11 12:45:59 +08:00   ❤️ 1
    https://github.com/SnapKit/Masonry/blob/master/Examples/Masonry%20iOS%20Examples/MASExampleAnimatedView.m#L105

    你那个宽度用变量控制会更好。

    AutoLayout 动画属于常识问题。。。
    xi_lin
        2
    xi_lin  
    OP
       2015-05-11 13:17:00 +08:00
    @vincentxue 感谢指教。新手上路还没有看过animation guide..

    另请教一下,我看到Masonry的example里
    ```
    // tell constraints they need updating
    [self setNeedsUpdateConstraints];

    // update constraints now so we can animate the change
    [self updateConstraintsIfNeeded];

    [UIView animateWithDuration:0.4 animations:^{
    [self layoutIfNeeded];
    }];
    ```
    需要这样先setNeedsUpdateConstraints再updateConstraintsIfNeeded吗?实验了一下只执行[self setNeedsUpdateConstraints];也是可以正常更新的
    vincentxue
        3
    vincentxue  
       2015-05-11 18:27:39 +08:00   ❤️ 1
    @xi_lin 不客气。

    这样调用是为了确保更改后的布局完成。

    你可以这样理解:

    setNeedsUpdateConstraints --> updateConstraintsIfNeeded --> updateConstraints。

    任何布局的改变都会触发 setNeedsUpdateConstraints 方法,这个方法告诉系统这个视图布局需要更新,系统会在稍后异步调用 updateConstraintsIfNeeded。调用的时间点一般是下一次 runloop 开始。

    updateConstraintsIfNeeded 方法告诉系统立即更新此视图的布局,内部调用了 updateConstraints,这里面写得是你的布局代码。

    所以如果你调用了 setNeedsUpdateConstraints,相当于延迟调用了 updateConstraintsIfNeeded,相当于间接调用了 updateConstraints。

    其实你直接调用 updateConstraints 也是可以的,至于为什么要有这样的一个调用顺序,这些方法内部肯定不是上面说的这么简单,系统会帮你处理很多问题,最好按照这个方法去调用就对了。
    xi_lin
        4
    xi_lin  
    OP
       2015-05-11 21:25:06 +08:00 via iPhone
    @vincentxue 感谢解释,懂了!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3313 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 12:44 · PVG 20:44 · LAX 05:44 · JFK 08:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.