美文网首页动画相关iOS
Masonry布局约束的UIView动画使用方法

Masonry布局约束的UIView动画使用方法

作者: justinjing | 来源:发表于2017-03-26 11:37 被阅读199次

    在没有自动布局之前我们都是用Frame去布局view,然后可以对view 做各种动画

    • 大小变化(frame)
    • 拉伸变化(bounds)
    • 中心位置(center)
    • 旋转(transform)
    • 透明度(alpha)
    • 背景颜色(backgroundColor)
    • 拉伸内容(contentStretch)

    但是有了自动布局之后,之前的方法稍微有所不同, 具体实现如下:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UIView *tview = [[UIView alloc] init];
        tview.backgroundColor = [UIColor redColor];
        [self.view addSubview:tview];
        
        [tview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tview.superview).offset(200);
            make.left.equalTo(tview.superview).offset(50);
            make.right.equalTo(tview.superview).offset(-100);
            make.height.equalTo(80);
        }];
    
        self.testView = tview;
        
        [self performSelector:@selector(startAnimation) withObject:nil afterDelay:1];
    }
    
    
    - (void)startAnimation{
        //重点
        [self.testView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.testView.superview).offset(300);
        }];
    
        [UIView animateWithDuration:0.66 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
             //重点
            [self.testView.superview layoutIfNeeded];
        } completion:^(BOOL finished) {
            
        }];
    }
    

    从最终效果来看好像没有什么问题,但是注意我们对top用了mas_updateConstraints,但是没有uninstall,这样的话有时候会有约束冲突,保险的是先[self.testViewTop uninstall],然后mas_updateConstraints,具体实现如下:

    @property (nonatomic, strong) MASConstraint *testViewTopConst;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UIView *tview = [[UIView alloc] init];
        tview.backgroundColor = [UIColor redColor];
        [self.view addSubview:tview];
        
        [tview mas_makeConstraints:^(MASConstraintMaker *make) {
            self.testViewTopConst = make.top.equalTo(tview.superview).offset(200);//重点
            make.left.equalTo(tview.superview).offset(50);
            make.right.equalTo(tview.superview).offset(-100);
            make.height.equalTo(80);
        }];
    
        self.testView = tview;
        
        [self performSelector:@selector(startAnimation) withObject:nil afterDelay:1];
    }
    
    
    - (void)startAnimation{
        [self.testViewTopConst uninstall];//重点
        [self.testView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.testView.superview).offset(300);
        }];
    
        [UIView animateWithDuration:0.66 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [self.testView.superview layoutIfNeeded];
        } completion:^(BOOL finished) {
            
        }];
    }
    

    除过这个方法还有一种更简单,安全的,就是我们把我们需要改变的约束用系统的NSLayoutConstraint来添加约束,最终只要修改constant值就行,具体如下:

    @property (nonatomic, strong) NSLayoutConstraint *topConst;//重点
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UIView *tview = [[UIView alloc] init];
        tview.backgroundColor = [UIColor redColor];
        [self.view addSubview:tview];
        
        [tview mas_makeConstraints:^(MASConstraintMaker *make) {
            //make.top.equalTo(tview.superview).offset(200);
            make.left.equalTo(tview.superview).offset(50);
            make.right.equalTo(tview.superview).offset(-100);
            make.height.equalTo(80);
        }];
        
        
        NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:tview
                                                                         attribute:NSLayoutAttributeTop
                                                                         relatedBy:NSLayoutRelationEqual
                                                                            toItem:tview.superview
                                                                         attribute:NSLayoutAttributeTop
                                                                        multiplier:1
                                                                          constant:50];
    
    
        
        [self.view addConstraint:topConstraint];//重点
        self.topConst = topConstraint;//重点
        
        self.testView = tview;
        
        [self performSelector:@selector(startAnimation) withObject:nil afterDelay:1];
    }
    
    
    - (void)startAnimation{
        self.topConst.constant = 300;//重点
        [UIView animateWithDuration:0.66 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [self.testView.superview layoutIfNeeded];
        } completion:^(BOOL finished) {
            
        }];
    }
    

    总结:

    • 方法一:直接用 mas_updateConstraints
    • 方法二:先uninstall,然后mas_updateConstraints
    • 方法三; 用系统的NSLayoutConstraint添加约束,然后修改constant值。

    建议使用第三种简单,安全

    相关文章

      网友评论

        本文标题:Masonry布局约束的UIView动画使用方法

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