美文网首页
10.4 Masonry使用-动画

10.4 Masonry使用-动画

作者: 草根小强 | 来源:发表于2019-04-26 16:56 被阅读0次

Masonry使用-动画

#import "ViewController.h"
#import "Masonry.h"

@interface ViewController ()


@property (nonatomic,strong) UIView *redView;

@property (nonatomic,strong) UIView *greenView;

@property (nonatomic,strong) UIView *blueView;

@property (nonatomic,assign) CGFloat padding;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.redView = [[UIView alloc]init];
    self.redView.backgroundColor = [UIColor redColor];
    self.redView.layer.borderColor = [UIColor blackColor].CGColor;
    self.redView.layer.borderWidth = 2.0f;
    [self.view addSubview:self.redView];
    
    self.blueView = [[UIView alloc]init];
    self.blueView.backgroundColor = [UIColor blueColor];
    self.blueView.layer.borderColor = [UIColor blackColor].CGColor;
    self.blueView.layer.borderWidth = 2.0f;
    [self.view addSubview:self.blueView];
    
    self.greenView = [[UIView alloc]init];
    self.greenView.backgroundColor = [UIColor greenColor];
    self.greenView.layer.borderColor = [UIColor blackColor].CGColor;
    self.greenView.layer.borderWidth = 2.0f;
    [self.view addSubview:self.greenView];
    
    //默认一个值
    self.padding = 10;
    
    [self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.view).offset(10);
//        make.top.mas_equalTo(self.view).offset(10);
        make.left.top.mas_equalTo(10);
        make.right.mas_equalTo(self.blueView.mas_left).offset(-10);
        make.bottom.mas_equalTo(self.greenView.mas_top).offset(-10);
    }];
    
    [self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(10);
        make.bottom.mas_equalTo(self.greenView.mas_top).offset(-10);
        make.width.mas_equalTo(self.redView);
        //三个视图的高相等
        make.height.mas_equalTo(@[self.redView,self.greenView]);
    }];
    
    [self.greenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
//        make.right.mas_equalTo(-10);
//        make.bottom.mas_equalTo(-10);
        make.right.bottom.mas_equalTo(-10);
        //下面这个约束可以不写 因为redView和blueView已经设置了这个约束
    //make.top.mas_equalTo(self.redView.mas_bottom).offset(-10);
    }];
    
    [self upadaThreeView];
}

- (void)upadaThreeView{
    
    CGFloat f = (self.padding>10)?10:300;
    //更新约束
    [self.greenView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(-f);
//        make.top.mas_equalTo(@[self.redView,self.blueView]).offset(f);
    }];
    
    //如果需要更新约束,需要调用这个方法
    [self.view setNeedsUpdateConstraints];
    //更新约束调用这个方法
    [self.view updateConstraintsIfNeeded];
    
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
        self.padding = f;
    } completion:^(BOOL finished) {
        [self upadaThreeView];
    }];
}

@end

会进行上下缩放

Masonry使用-动画1.png
Masonry使用-动画2.png

相关文章

网友评论

      本文标题:10.4 Masonry使用-动画

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