Autolayout constraint实现动画效果

2016-01-07  本文已影响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];
    }];

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

上一篇 下一篇

猜你喜欢

热点阅读