[英]Presenting a View Controller Modally Resets The Current View
I am trying to present view controller modally (a UIImagePickerController to be precise) while animating some views before the presentation. 我正在尝试以模态方式呈现视图控制器(准确地说是一个UIImagePickerController),同时在呈现之前对一些视图进行动画处理。
It's important to note that the animations and the presentation of the view are not chained, and there can be a time-gap between the views animating and modally presenting the image picker. 重要的是要注意,动画和视图的表示没有链接在一起,并且在动画和模态呈现图像选择器的视图之间可能存在时间间隔。
With all that said, while I present the view controller, it seems that all of the subviews of the current view I'm in (the view I'm presenting from - the parent view controller's view) are being reset to their original "storyboard" positions, which causes all of the animations I've done in the view BEFORE presenting the image picker to reset. 话虽如此,当我展示视图控制器时,似乎我所在的当前视图的所有子视图(我正在展示的视图-父视图控制器的视图)都被重置为其原始的“ storyboard” ”位置,这会导致我在呈现图像选择器之前重置视图中所做的所有动画。
Another thing worth noting, is that I use AutoLayout to position the views I animate. 另一点值得注意,是我使用AutoLayout来放置我设置动画的视图。
I've added a sample animation showing the problem - notice how the "Animate" button snaps back to it's original position right after I click the "Present Modal View Controller" button (I've toggled "Slow Animations" right before presenting the modal view controller so you can see the "Animate" button snaps back). 我添加了一个演示该问题的动画示例-注意在单击“当前模态视图控制器”按钮后,“动画”按钮是如何捕捉回到其原始位置的(在显示模态之前,我已经切换了“慢速动画”视图控制器,以便您可以看到“动画”按钮的回弹)。
I'm also adding a link to the example app shown in the animation so you examine the problem more deeply, click here to download it. 我还在动画中显示的示例应用程序中添加了链接,以便您更深入地研究问题, 请单击此处下载它。
To solve the issue, I used @kokx's answer, and animated the Auto Layout constraints of the views instead of their frame values. 为了解决该问题,我使用了@kokx的答案,并对视图的“自动布局”约束(而不是其帧值)进行了动画处理。 To do that, I simply created outlets for the constraints I wanted to modify, and modified the
constant
property of the constraints. 为此,我只是为要修改的约束创建了出口,并修改了约束的
constant
属性。
To animate the change, simply call the original [UIView animateWithDuration:options:animations:completion:]
while replacing any animation code it's animations:
part with a call to [self.view layoutIfNeeded]
. 要对更改进行动画处理,只需调用原始的
[UIView animateWithDuration:options:animations:completion:]
同时替换其动画的任何动画代码animations:
调用[self.view layoutIfNeeded]
。
To solve that issue, you need to use NSLayoutConstraint
as well as change Constraint instead of changing frames. 要解决该问题,您需要使用
NSLayoutConstraint
以及更改Constraint而不是更改框架。
Please check attached video where, Issue is resolved. 请查看附件中已解决问题的视频。 Video Link
影片连结
Code will be as below: 代码如下:
self.topcons.constant = self.topcons.constant - 220;
[UIView animateWithDuration:0.4f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.view layoutIfNeeded];
} completion:nil];
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.