UIViewanimation一个约束会影响另一个约束

我有一个UIImageView,我想在后台使用泛效应缓慢animation。 我的背景图片视图有四个约束:

  • 从顶部到顶部布局指南的距离(零)
  • 从底部到底部布局指南的距离(零)
  • 宽度(恒定在850)
  • 引导空间超级查看(零,连接到代码为backgroundImageViewLeftMargin outlet)

图像视图的视图模式设置为“纵横填充”。 当我运行我的应用程序,没有问题,静态图像显示。 我添加下面的代码来animation我的视图:

 -(void)animateBackgroundHorizontally:(BOOL)forward{ self.backgroundImageViewLeftMargin.constant = forward ? -500 : 0; [self.backgroundImage setNeedsUpdateConstraints]; [UIView animateWithDuration:30 animations:^{ [self.backgroundImage layoutIfNeeded]; } completion:^(BOOL finished) { [self animateBackgroundHorizontally:!forward]; }]; } 

然后我调用[self animateBackgroundHorizontally:YES]; 在… viewWillAppear: 。 这是发生了什么事情:

在这里输入图像说明

图像从一个意想不到的(但总是相同的)位置开始animation(顶部边缘位于屏幕的中间位置,应该在屏幕的顶部),正确地animation到其最终位置,然后在animation返回到原始位置时位置,它确实animation正确 。 我animation的唯一属性是左边距。 当我不调用animation代码时,它显示正确。 为什么animation从图像上看到的位置不正确? 我试过将self.backgroundImage.translatesAutoresizingMaskIntoConstraints设置为YES (我无法同时满足constaints错误)和NO但它没有工作。 这个问题在iOS 6和iOS 7上都是持久的。

我把animation的开始代码放在viewDidAppear:而不是viewWillAppear:问题就没有了。 Insteresting。 尝试在iOS 6和iOS 7上,他们现在都工作得很好。