使用自动布局时,相当于"CGAffineTransformMakeTranslation(0,0)"?动画回到原始位置

Mic*_*all 3 ios autolayout

我试图动画从位置A到B然后再返回的视图.以前,我会做以下的动画来制作B:

[UIView animateWithDuration:1 animations:^{
    self.myView.transform = CGAffineTransformMakeTranslation(100, 0);
}];
Run Code Online (Sandbox Code Playgroud)

然后这将动画回到A:

[UIView animateWithDuration:1 animations:^{
    self.myView.transform = CGAffineTransformMakeTranslation(0, 0);
}];
Run Code Online (Sandbox Code Playgroud)

所有这些都无需了解原始位置.

现在在自动布局中我使用以下代码动画到位置B:

self.myLeadingConstraint.constant = 100;

[UIView animateWithDuration:1 animations:^{
    [self.view layoutIfNeeded];
}];
Run Code Online (Sandbox Code Playgroud)

有没有办法获得以前的常量值而无需创建另一个变量或查看IB以查看初始值是什么?有没有更好的方法来做所有这些?

提前致谢.

Jan*_*ano 5

不,这是最简单的方法.以下是动画自动布局的选项:

  • 在块中调用layoutIfNeeded.如果您只是翻译,只更改常量.
  • 动画图层而不是视图.
  • 删除约束,使用自动调整掩码.
  • 使用容器视图并为该容器设置动画.
  • 使用不会干扰的约束.
  • 在viewDidLayoutSubviews中设置框架.

你的意思是获得前一个恒定值?是不是在self.myLeadingConstraint.constant上呢?