繁体   English   中英

UIView没有改变animateWithDuration中的位置

[英]UIView not changing position in animateWithDuration

我有两个UIViews(我很糟糕,它是一个UIView和一个UIButton ),我在同一时间制作动画。 我最初有一个视图和一个容器视图,它会动画很好,像魅力一样工作。

现在只有我的一个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”调试代码都返回相同的值,这表明框架确实移动了。 是否有一个特定的原因,当它作为UIContainerView工作时,当toggleUIView时,这将不起作用?

请注意,如果我删除该行

self.toggle.frame = rect;

tabButton将正确设置动画,但如果我移动toggletabButton将不会移动,无论它是动画块中的第一个还是第二个。

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

编辑2:图片证据。{

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

初始位置(<code> toggle </ code>在屏幕外)正确位置

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

问题<code> toggle </ code>的问题是正确的<code> tabButton </ code>不是

上图:问题toggle的问题是正确的tabButton不是

当<code> self.toggle.frame = rect </ code>被注释掉时(<code> tabButton </ code>正确,<code> toggle </ code>没有)

上图:当self.toggle.frame = rect时( tabButton正确, toggle不到)}

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

我已经做了一些测试,即使我将动画块中的toggle更改为瞬间, tabButton 仍然没有动画。 这让我觉得tabButton可能只是从根本上不喜欢toggle视图和/或我自己,所以不会只是为了惹我tabButton

}

编辑4:{

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

}

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

  • toggleToggleDraw一个实例,它是我创建的UIView的子视图。

  • tabButton是一个UIButton ,它是我的IB viewController的一部分,是该类的一个属性

  • 无论toggletabButton是子视图self.view

  • 动画将单独工作,不会修改rects的逻辑,但如果它们同时动画则不起作用

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

我在IB中创建的UIView的动画有问题(动画没有从视图的当前位置开始,并且在初始位置结束)。

将layoutIfNeeded()发送到动画块之前的底层视图后,一切正常:

self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5) { () -> Void in
...

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

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

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!");
}];

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

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

如果有人知道为什么视图不会在界面构建器中创建按钮时动画在这里发布答案,我有兴趣知道这背后的原因。

谢谢大家的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM