美文网首页
Autolayout constraint实现动画效果

Autolayout constraint实现动画效果

作者: 深山问 | 来源:发表于2016-01-07 13:19 被阅读212次
  1. 更改frame实现动画效果
    对于没有添加约束的控件来说,要想到达动画移动的效果,一般情况下,我们是更改控件的frame值来达到动画效果的。
    如下
@property (nonatomic, strong) UIView *lastView;

实现:

    CGRect tempFrame = self.lastView.frame;
    tempFrame.origin.y += 100;
    [UIView animateWithDuration:0.2f animations:^{
        CGRect tempFrame = self.lastView.frame;
        tempFrame.origin.y += 100;
        self.lastView.frame = tempFrame;
    }];

2.对于添加约束的控件来说,更改frame值并不能实现动画效果。
因此我们可以根据更改“约束”来实现动画效果:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *showViewTopViewConstraint;

实现:

    self.showViewTopViewConstraint.constant = 200;
    [UIView animateWithDuration:0.2f animations:^{
        [self.view layoutIfNeeded];
    }];

这样就可以利用约束实现动画效果了。

相关文章

网友评论

      本文标题:Autolayout constraint实现动画效果

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