美文网首页iOS进阶之路
masonry 使用 UIView 的动画

masonry 使用 UIView 的动画

作者: 黑暗森林的歌者 | 来源:发表于2017-04-14 16:47 被阅读1205次

masonry 使用 UIView 的动画
在创建一个 view 的时候,给这个 view 一个从下弹出的动画,以前使用 frame 做布局的时候,很简单的就用 UIViewAnimation 就做出来了,可是现在APP 布局用的是 masonry,直接在设置好初始 layout 后就更新,发现并没有动画,而是直接就显示了出来,后来发现是需要 view 先更新第一次的约束,动画执行后在执行一个更新约束才能出现动画
代码如下(项目中的代码,没有做整理,理解理解思路就行):

- (GoodDetailSelectNumView *)selectNumView {
    if (!_selectNumView) {
        _selectNumView = [[GoodDetailSelectNumView alloc] init];
        [self.view addSubview:_selectNumView];
        [_selectNumView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(150);
            make.width.equalTo(self.view);
            make.height.mas_equalTo(150);
        }];
        // 注意需要先执行一次更新约束
        [self.view layoutIfNeeded];
        
        __weak typeof(self) weakSelf = self;
        _selectNumView.exitSelectNum = ^{
            [weakSelf removeSelectView];
        };
        [UIView animateWithDuration:0.3 animations:^{
            [_selectNumView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(-50);
            }];
            // 注意需要再执行一次更新约束
            [self.view layoutIfNeeded];
        }];
    }
    return _selectNumView;
}
示例
- (void)removeSelectView {
    [UIView animateWithDuration:0.3 animations:^{
        [_selectNumView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(150);
        }];
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [_selectNumView removeFromSuperview];
        _selectNumView = nil;
    }];
}

相关文章

网友评论

  • ArchLL:建议先调用setNeedsLayout再调用layoutIfNeeded
  • fuadam1982:确实有效,不过用mas_make就可以更新动画了。用mas_update还是有点问题

本文标题:masonry 使用 UIView 的动画

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