【问题标题】:UIView not changing position in animateWithDurationUIView 在 animateWithDuration 中没有改变位置
【发布时间】:2014-02-13 08:21:19
【问题描述】:

我有两个 UIViews(我的错误是 UIViewUIButton),我正在同时制作动画。我最初有一个视图和一个 containerView,它可以很好地制作动画并且像魅力一样工作。

现在只有我的一个 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 会正确动画,但如果我移动toggletabButton 将不会移动,无论它是在动画块中的第一个还是第二个。

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

编辑 2:图片证据。{

在以下截图中,tabButton bg 为绿色,toggle bg 为红色。

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

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

上图:当self.toggle.frame = rect 被注释掉时(tabButton 正确,toggle 不正确) }

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

我已经做了一些测试,即使我将 toggle 更改为动画块以使其成为即时的东西,tabButton 仍将仍然没有动画。这让我觉得tabButton 可能根本不喜欢toggle 的观点和/或我自己,所以不会仅仅为了惹恼我而采取行动。

}

编辑 4:{

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

}

我最好添加更多簿记/TLDR 信息以防不清楚。

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

  • tabButtonUIButton,它是我的 IB viewController 的一部分,也是该类的属性

  • toggletabButton 都是self.view 的子视图

  • 动画将单独工作,无需修改矩形的逻辑,但如果它们同时动画则将无法工作

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

【问题讨论】:

  • 至少作为一个临时的hack,如果你把它们放在不同的块中会发生什么?
  • 除非切换行被注释掉,否则仍然无法工作
  • 有趣。你能检查一下这两个按钮的属性有什么不同吗?
  • 只有一个是按钮(tabButton),另一个是我从笔尖(切换)加载的视图。除此之外,属性或多或少是相同的。
  • 如果你不改变它的大小,试着移动它的中心点? self.toggleButton.center

标签: ios objective-c animation uiview


【解决方案1】:

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

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

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

【讨论】:

  • 如果我把它放在 Swift 2.1 的动画块中对我来说效果最好
【解决方案2】:

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

【讨论】:

  • 这个我试过了,动画基本上无缘无故地倒转了
  • 切换的子视图 contian tabButton?
  • 不,视图是同级的。两者都是 self.view 的子视图
【解决方案3】:

我已经创建了一个您所描述的示例,并且一切似乎都运行良好。这是我用的:

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

我可以建议的是确保代码在主线程上运行:

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

【讨论】:

  • 这是tabButtonUIButtontoggleUIView 吗?我将视图放在与 tabButton 相同的 Rect 中,并且视图移动得很好。
  • 我刚刚编辑,添加了我用于测试的内容。一切正常。通常当我遇到这种奇怪的动画行为时,它几乎总是与在后台线程而不是主线程上运行的代码有关。
【解决方案4】:

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

然而,这对我来说似乎非常hacky,有点像在缺失的脚上贴上创可贴,然后希望它能自行解决。 我无法找出问题的根本原因,但至少我可以避免症状。

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

感谢大家的帮助。

【讨论】:

  • 问你的问题是你在哪里使用 UIDynamicAnimator 吗?
猜你喜欢
  • 2015-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多