UIView没有改变animateWithDuration中的位置

时间:2014-01-23 00:39:15

标签: ios objective-c animation uiview

我有两个UIViews(我的不好是UIViewUIButton)我同时在动画。我最初有一个视图和一个容器视图,它会动画很好,像魅力一样工作。

现在只有一个我的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);

切换视图移动正常,但tabButton视图没有动画或移动。奇怪的是,“After”和“AfterAnimation”调试代码都返回相同的值,这表明框架确实移动了。当toggleUIView时,UIContainerView作为self.toggle.frame = rect; 工作时,是否有一个特定的原因?

请注意,如果我删除该行

tabButton

toggle会动画正确,但如果我移动tabButtontoggle将无法移动,无论它是动画块中的第一个还是第二个。

编辑:我已经尝试将它们移动到单独的块中并更改中心点而不是框架,但无济于事。似乎如果tabButton视图移动,toggle将不会移动。

编辑2:图片证据。{

在以下屏幕截图中,tabButton bg为绿色,切换bg为红色。

Initial position (<code>toggle</code> is off-screen) correct position

上方:初始位置(toggle在屏幕外)正确位置

The problem in question <code>toggle</code> is correct <code>tabButton</code> is not

上图:问题tabButton中的问题是正确的self.toggle.frame = rect不是

When <code>self.toggle.frame = rect</code> is commented out (<code>tabButton</code> correct, <code>toggle</code> not)

上方:当tabButton被注释掉时(toggle正确,toggle没有) }

编辑3:这比我担心的还要糟糕。{

我已经做了一些测试,即使我从动画块中取出tabButton更改以使其成为即时内容,tabButton 动画。这让我觉得toggle可能只是从根本上不喜欢tabButton视图和/或我自己,所以不会只是为了惹我生气。

}

编辑4:{

如果我将tabButton.frame = CGRectMake(10,10,100,100)动画更改为toggle,视图会立即捕捉到该位置,并在动画持续时间的同时动画回原始位置。

}

如果事情不明确,我最好添加更多簿记/ TLDR信息。

  • ToggleDrawUIView的一个实例,是我创建的tabButton的子视图。

  • UIButtontoggle,是我的IB viewController的一部分,是该类的属性

  • tabButtonself.view都是toggle

  • 的子视图
  • 动画将单独工作,不会修改rects的逻辑,但如果同时动画,则无效

  • 无论顺序如何,
  • tabButton动画似乎优先于{{1}}动画

3 个答案:

答案 0 :(得分:0)

我认为这不是UIView动画的问题。也许你的切换位置与你的tabButton有关。试一试,您可以将切换框设置为直接舔(10,10,100,100),然后检查结果。

答案 1 :(得分:0)

我已经创建了一个你所描述的例子,一切似乎都很好。这是我用过的:

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;
}];

我可以建议确保代码在mainthread上运行:

dispatch_async(dispatch_get_main_queue(), ^{
    [UIView animateWithDuration:0.3 animations:^{
        self.toggle.frame = rect; // move to new location
        self.tabButton.frame = tabRect;
    }];
});

还要考虑到动画代码后的日志不正确,因为它不会在动画后运行,而是紧挨着要求动画。

如果您想在动画之后运行代码,请使用:

[UIView animateWithDuration:0.3 animations:^{
    self.toggle.frame = rect; // move to new location
    self.tabButton.frame = tabRect;
} completion:^(BOOL finished){
    NSLog(@"Finished animating!");
}];

答案 2 :(得分:0)

我找到了解决问题的方法。如果我以编程方式而不是通过界面构建​​器初始化UIButtontabButton),则两个视图将同时进行动画处理。

然而,对我来说,这似乎非常 hacky ,有点像把一个绑匪放在一只失踪的脚上,并希望它会自行解决。 我无法解决问题的根本原因,但至少我可以避免这些症状。

如果有人知道为什么视图在界面构建器中按钮制作时没有动画,请在此处发布答案,我有兴趣了解其背后的原因。

感谢大家的帮助。

相关问题