[英]UIView not changing position in animateWithDuration
I have two UIViews (My bad it is a UIView
and a UIButton
) which I am animating at the same time. 我有两个UIViews(我很糟糕,它是一个UIView
和一个UIButton
),我在同一时间制作动画。 I originally had a view and a containerView which would animate just fine and worked like a charm. 我最初有一个视图和一个容器视图,它会动画很好,像魅力一样工作。
Now only one of my UIViews will move/animate in animateWithDuration even though through debugging the frame of the other view says that it is in a position it is not. 现在只有我的一个UIViews会在animateWithDuration中移动/动画,即使通过调试另一个视图的框架说它处于某个位置它不是。
CGRect rect = self.toggle.frame;
CGRect tabRect = self.tabButton.frame;
rect.origin.x = rect.origin.x - rect.size.width;
NSLog(@"%f Before",tabRect.origin.x);
tabRect.origin.x = tabRect.origin.x - rect.size.width;
NSLog(@"%f After", tabRect.origin.x);
[UIView animateWithDuration:0.3 animations:^{ // animate the following:
self.toggle.frame = rect; // move to new location
self.tabButton.frame = tabRect;
}];
NSLog(@"%f AfterAnimation", tabButton.frame.origin.x);
The toggle view moves fine, but the tabButton
view does not animate or move. 切换视图移动正常,但tabButton
视图不动画或移动。 The strange thing is that both the "After" and "AfterAnimation" debugging code returns the same value, which suggests the frame has indeed moved. 奇怪的是,“After”和“AfterAnimation”调试代码都返回相同的值,这表明框架确实移动了。 Is there a specific reason that this will not work when toggle
is a UIView
when it would work as a UIContainerView
? 是否有一个特定的原因,当它作为UIContainerView
工作时,当toggle
是UIView
时,这将不起作用?
Note that if I remove the line 请注意,如果我删除该行
self.toggle.frame = rect;
tabButton
will animate correctly, but if I move toggle
, tabButton
will not move regardless of whether it is first in the animation block or second. tabButton
将正确设置动画,但如果我移动toggle
, tabButton
将不会移动,无论它是动画块中的第一个还是第二个。
Edit: I have tried moving them into separate blocks and to change the center point rather than the frame, to no avail. 编辑:我已经尝试将它们移动到单独的块中并更改中心点而不是框架,但无济于事。 It seems that if the toggle
view moves, the tabButton
will not move. 似乎如果toggle
视图移动, tabButton
将不会移动。
Edit 2: The pictorial evidence.{ 编辑2:图片证据。{
In the following screenshots tabButton bg is green and toggle bg is red. 在以下屏幕截图中,tabButton bg为绿色,切换bg为红色。
Above: Initial position ( toggle
is off-screen) correct position 上图:初始位置( toggle
屏幕外)正确位置
Above: The problem in question toggle
is correct tabButton
is not 上图:问题toggle
的问题是正确的tabButton
不是
Above: When self.toggle.frame = rect
is commented out ( tabButton
correct, toggle
not) } 上图:当self.toggle.frame = rect
时( tabButton
正确, toggle
不到)}
Edit 3: It's even worse than I feared.{ 编辑3:这比我担心的还要糟糕。{
I have done a few more tests and even if I take the toggle
change out of the animation block to make it an instant thing, the tabButton
will still not animate. 我已经做了一些测试,即使我将动画块中的toggle
更改为瞬间, tabButton
仍然没有动画。 This makes me think the tabButton
may just fundamentally dislike the toggle
view and/or myself so will not move just to spite me. 这让我觉得tabButton
可能只是从根本上不喜欢toggle
视图和/或我自己,所以不会只是为了惹我tabButton
。
} }
Edit 4:{ 编辑4:{
If I change the tabButton
animation to tabButton.frame = CGRectMake(10,10,100,100)
the View snaps instantly to that location and animates back to its original position in the same time as the animation duration. 如果我将tabButton
动画更改为tabButton.frame = CGRectMake(10,10,100,100)
,View会立即捕捉到该位置,并在动画持续时间的同时动画回原始位置。
} }
I better add more bookkeeping/TLDR information in case things aren't clear. 如果事情不明确,我最好添加更多簿记/ TLDR信息。
toggle
is an instance of ToggleDraw
which is a subview of UIView
which I created. toggle
是ToggleDraw
一个实例,它是我创建的UIView
的子视图。
tabButton
is a UIButton
which is part of my IB viewController and a property of the class tabButton
是一个UIButton
,它是我的IB viewController的一部分,是该类的一个属性
Both toggle
and tabButton
are subviews of self.view
无论toggle
和tabButton
是子视图self.view
The animations will work individually with no modifications to the logic of the rects but will not work if they are animated at the same time 动画将单独工作,不会修改rects的逻辑,但如果它们同时动画则不起作用
toggle
animation seems to take precedence over tabButton
animation regardless of the order 无论顺序如何, toggle
动画似乎优先于tabButton
动画
I had a problem with the animation of an UIView created in IB (the animation didn't start from the current position of the view, and ended in the initial position). 我在IB中创建的UIView的动画有问题(动画没有从视图的当前位置开始,并且在初始位置结束)。
All worked fine after sending layoutIfNeeded() to the underlaying view before the animation block: 将layoutIfNeeded()发送到动画块之前的底层视图后,一切正常:
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5) { () -> Void in
...
I think it is not a problem about a UIView Animation. 我认为这不是UIView动画的问题。 Maybe your toggle posiztion is related to your tabButton. 也许你的切换位置与你的tabButton有关。 For a try, your can set toggle frame to a rect lick (10, 10, 100,100), then check the result. 试一试,您可以将切换框设置为直接舔(10,10,100,100),然后检查结果。
I've created an example of what you describe and everything seems to work fine. 我已经创建了一个你所描述的例子,一切似乎都很好。 This is what I used: 这是我用过的:
UIView *toggle = [[UIView alloc] initWithFrame:CGRectMake(320, 64, 100, 100)];
[toggle setBackgroundColor:[UIColor redColor]];
[self.view addSubview:toggle];
UIButton *tabButton = [[UIButton alloc] initWithFrame:CGRectMake(220, 64, 100, 100)];
[tabButton setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:tabButton];
CGRect rect = toggle.frame;
CGRect tabRect = tabButton.frame;
rect.origin.x = rect.origin.x - rect.size.width;
NSLog(@"%f Before",tabRect.origin.x);
tabRect.origin.x = tabRect.origin.x - rect.size.width;
NSLog(@"%f After", tabRect.origin.x);
[UIView animateWithDuration:1 animations:^{ // animate the following:
toggle.frame = rect; // move to new location
tabButton.frame = tabRect;
}];
What I can suggest is to make sure that the code is being ran on mainthread: 我建议的是确保代码在mainthread上运行:
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.3 animations:^{
self.toggle.frame = rect; // move to new location
self.tabButton.frame = tabRect;
}];
});
Also take into account that the log you have after the animation code is incorrect as it won't run after the animation, but rather right next to asking for the animation. 还要考虑到动画代码后的日志不正确,因为它不会在动画后运行,而是紧挨着要求动画。
If you want code to run after the animation you should use: 如果您希望代码在动画后运行,您应该使用:
[UIView animateWithDuration:0.3 animations:^{
self.toggle.frame = rect; // move to new location
self.tabButton.frame = tabRect;
} completion:^(BOOL finished){
NSLog(@"Finished animating!");
}];
I have found a solution to the problem. 我找到了解决问题的方法。 If I initialise the UIButton
( tabButton
) programmatically rather than through the interface builder, both views will animate simultaneously. 如果我以编程方式而不是通过界面构建器初始化UIButton
( tabButton
),则两个视图将同时进行动画处理。
This however seems very hacky to me, kind of like putting a bandaid over a missing foot and hoping it will sort itself out. 然而,这对我来说似乎非常讨厌 ,有点像在一只失踪的脚上放一个绑带,并希望它会自行解决。 I could not work out the root cause of the problem but at least I could avoid the symptoms. 我无法解决问题的根本原因,但至少我可以避免这些症状。
If anyone knows why the views would not animate when the button was made in the interface builder post an answer here, I am interested in knowing the reason behind this. 如果有人知道为什么视图不会在界面构建器中创建按钮时动画在这里发布答案,我有兴趣知道这背后的原因。
Thanks for your help everyone. 谢谢大家的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.