Masonry 更新约束,实现动画效果

#import "ViewController.h"  
#import "Masonry.h"  
  
@interface ViewController ()  
@property(nonatomic,strong)UITextField *greenTextView;  
@property(nonatomic,strong)UIButton *blueBtn;  
@end  
  
@implementation ViewController  
  
- (void)viewDidLoad {  
    [super viewDidLoad];  
      
    //初始化子控件  
    [self setupSubviews];  
      
    //键盘弹出监听  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];  
    //键盘收回监听  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];  
}  
  
/** 
 初始化子控件 
 */  
-(void)setupSubviews  
{  
    //文本框textview  
    UITextField *greenTextView=[[UITextField alloc]init];  
    greenTextView.backgroundColor = [UIColor greenColor];  
    greenTextView.placeholder = @"点我更新约束啦...";  
    self.greenTextView = greenTextView;  
    [self.view addSubview:greenTextView];  
      
    //设置初始时的约束  
    //左边距离self.view左边10  
    //底部距离self.view 20  
    //高度为50  
    //宽度自适应  
    [greenTextView mas_makeConstraints:^(MASConstraintMaker *make) {  
        make.leading.equalTo(self.view.mas_leading).offset(10);  
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);  
        make.height.mas_equalTo(50);  
    }];  
      
    //按钮  
    UIButton *blueBtn = [[UIButton alloc]init];  
    blueBtn.backgroundColor = [UIColor blueColor];  
    self.blueBtn = blueBtn;  
    [self.view addSubview:blueBtn];  
      
    //设置初始时的约束  
    //右边距离self.view右边10  
    //左边距离self.greenTextView右边 10  
    //宽高和self.greenTextView一样  
    [blueBtn mas_makeConstraints:^(MASConstraintMaker *make) {  
        make.trailing.equalTo(self.view.mas_trailing).offset(-10);  
        make.leading.equalTo(self.greenTextView.mas_trailing).offset(10);  
        make.bottom.equalTo(self.greenTextView.mas_bottom);  
        make.size.equalTo(self.greenTextView);  
    }];  
}  
  
//键盘弹出时会调用  
-(void)keyboardWillShow:(NSNotification *)notification  
{  
    //获取键盘的基本信息  
    NSDictionary *userInfo = [notification userInfo];  
    CGRect rect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];  
      
    CGFloat keyboardHeight = CGRectGetHeight(rect);  
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];  
      
    //修改下边距约束  
    [self.greenTextView mas_updateConstraints:^(MASConstraintMaker *make) {  
        make.bottom.mas_equalTo(-keyboardHeight-20);  
    }];  
      
    //更新约束  
    [UIView animateWithDuration:duration animations:^{  
        [self.view layoutIfNeeded];  
    }];  
}  
  
//键盘收回时会调用  
-(void)keyboardWillHidden:(NSNotification *)notification  
{  
    NSDictionary *userInfo = [notification userInfo];  
  
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];  
      
    //修改下边距约束  
    [self.greenTextView mas_updateConstraints:^(MASConstraintMaker *make) {  
        make.bottom.mas_equalTo(-20);  
    }];  
      
    //更新约束  
    [UIView animateWithDuration:duration animations:^{  
        [self.view layoutIfNeeded];  
    }];  
}  
  
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
{  
    //结束编辑  
    [self.view endEditing:YES];  
}  

你可能感兴趣的:(Masonry 更新约束,实现动画效果)