Masonary 更新约束时怎么能带上动画效果呢? - V2EX
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 年 5 月 11 日 5185 次点击
    这是一个创建于 3990 天前的主题,其中的信息可能已经有所发展或是发生改变。

    根据文档在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 年 5 月 11 日   1
    https://github.com/SnapKit/Masonry/blob/master/Examples/Masonry%20iOS%20Examples/MASExampleAnimatedView.m#L105

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

    AutoLayout 动画属于常识问题。。。
    xi_lin
        2
    xi_lin  
    OP
       2015 年 5 月 11 日
    @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 年 5 月 11 日   1
    @xi_lin 不客气。

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

    你可以这样理解:

    setNeedsUpdateConstraints --> updateConstraintsIfNeeded --> updateConstraints。

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

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

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

    其实你直接调用 updateConstraints 也是可以的,至于为什么要有这样的一个调用顺序,这些方法内部肯定不是上面说的这么简单,系统会帮你处理很多问题,最好按照这个方法去调用就对了。
    xi_lin
        4
    xi_lin  
    OP
       2015 年 5 月 11 日 via iPhone
    @vincentxue 感谢解释,懂了!
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5250 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 01:11 PVG 09:11 LAX 18:11 JFK 21:11
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86