@property (nonatomic,strong) MASConstraint *topConstraint; // 创建view1居上距离属性
@property (nonatomic,strong) UIView *view1;
self.view1 = [[UIView alloc] init];
self.view1.translatesAutoresizingMaskIntoConstraints = NO; // 要先赋值为NO
self.view1.backgroundColor = [UIColor greenColor];
#pragma mark 注:要使用autoLayout,必须先将页面addSubview,否则报错
[self.view addSubview:self.view1];
// 屏宽、高30、居父类左0上10
[self.view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(self.view1.superview.mas_width);
make.height.equalTo(@30);
make.left.equalTo(self.view1.superview.mas_left).with.offset(0);
// view1居上距离属性赋值
self.topConstraint = make.top.equalTo(self.view1.superview.mas_top).with.offset(50);
}];
#pragma mark 点击按钮,执行动画
- (void)buttonClick:(UIButton *)sender {
[UIView animateWithDuration:2 animations:^{
self.topConstraint.offset = 100; // 从新设置view1的居上的偏移量,使其等于100
[self.view layoutIfNeeded]; // 只有添加了这个方法,才能有动画
}];
}