UIView和Masonry实现动画效果

Masonry 实现动画效果如下:

//button点击方法
- (void)clickedButton
{
    static BOOL isMove; //默认是NO
    Weakify(weakSelf);
    
    //告诉self.view约束需要更新
    [weakSelf.view setNeedsUpdateConstraints];
    //调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
    [weakSelf.view updateConstraintsIfNeeded];
    
    if (isMove) {
        
        isMove = NO;
        //添加动画
        [UIView animateWithDuration:5 animations:^{
            [weakSelf.displayView mas_updateConstraints:^(MASConstraintMaker *make) {
                //更改距顶上的高度
                make.top.equalTo(weakSelf.baseView.mas_bottom).with.offset(100);
            }];
            
            //必须调用此方法,才能出动画效果
            [weakSelf.view layoutIfNeeded];
        }];
    }
    else{
        
        isMove = YES;
        //添加动画
        [UIView animateWithDuration:5 animations:^{
            
            [weakSelf.displayView mas_updateConstraints:^(MASConstraintMaker *make) {
                //更改距顶上的高度
                make.top.equalTo(weakSelf.baseView.mas_bottom).with.offset(-100);
            }];
            //必须调用此方法,才能出动画效果
            [weakSelf.view  layoutIfNeeded];
        }];
    }
}

重点说明:

相关推荐