【问题标题】:Modifying an iPhone animation container view before starting animation在开始动画之前修改 iPhone 动画容器视图
【发布时间】:2010-11-30 03:56:26
【问题描述】:

我正在为我正在开发的纸牌游戏添加一些基本动画。 (我的第一个 iPhone 应用。)

我正在创建一个自定义 UIView 类“AnimationContainer”,它从 image1 翻转到 image2,同时从 rect1 移动到 rect2。我的最终目的是让最多四个这样的容器同时进行转换。

我遇到的问题是动画没有显示 image1...所以只出现了翻转过渡的后半部分。

但是,如果我先通过触摸“重置”来重置动画,那么一切都会完美运行。换句话说,如果我一次又一次地按下翻转,我只会得到一半的过渡......但是如果我先按下重置,那么一切都可以完美地进行一次翻转。

那么,我怎样才能让动画正确重置呢?

以下是代码、屏幕截图和完整链接:Project Zip File 700k

- (void)displayWithImage1 {     //RESET button calls this
    self.frame = rect1;
    [image2 removeFromSuperview];
    [self addSubview:image1];
    [self setNeedsDisplay]; //no help: doesn't force an update before animation
}

- (void)runTheAnimation {     //FLIP button calls this
    [self displayWithImage1]; //<---this is what the reset button calls
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:transition forView:self cache:NO];
    self.frame = rect2;
    [image1 removeFromSuperview];
    [self addSubview:image2];
    [UIView commitAnimations];
}

谢谢!

【问题讨论】:

  • 我想出了一个半丑陋的解决方法。我添加了一个“虚拟”动画,并让我的班级成为其 setAnimationDidStopSelector 的代表。虚拟动画的持续时间为 0.0,只是将视图移动到 rect1。当调用假人的停止选择器时,我会从上面执行“真实”动画翻转代码。功能齐全,看起来至少可以同时运行 4 个。耶,我。哈哈
  • 事实证明,解决方法可以产生长达半秒的延迟,所以我开始赏金了。必须有一种方法可以向容器视图发送两个图像,并使其在一次转换中从一个翻转到下一个。

标签: iphone objective-c cocoa-touch animation uikit


【解决方案1】:

您需要一个绘图循环来传递,以便在执行动画之前重绘视图。这段代码是“画这个,当下一个事件循环出现时,做其他事情”的一个例子。在 UI 代码中执行此操作并不少见。您的第一个解决方法是尝试同样的事情,但方式要复杂得多。

- (void)_runTheAnimation {
    // Moved here from -runTheAnimation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:transition forView:self cache:NO];
    self.frame = rect2;
    [image1 removeFromSuperview];
    [self addSubview:image2];
    [UIView commitAnimations];
}

- (void)runTheAnimation {     //FLIP button calls this
    [self displayWithImage1];
    [self performSelector:@selector(_runTheAnimation) withObject:nil afterDelay:0.0];
}

【讨论】:

  • 太棒了!太感谢了。我完全不清楚选择器没有立即执行。现在一切都很好。 200 个代表的赏金是一个很小的代价。我花了 10 个小时搞砸这个。
  • 选择器在被要求时立即发送 (-peformSelector:) 上面的方法是 -performSelector:withObject:afterDelay:,它在运行循环中安排选择器在将来的某个时间发送。延迟 0 实际上意味着“下一次运行循环迭代”。选择器本身只是描述要传递给对象的消息。传递该消息的行为由 performSelector... 方法完成。
  • 酷,我想我明白了。仍在学习 Cocoa、Objective-C 和 iPhone 的所有东西……但我现在学得更快了。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-28
  • 2011-07-11
  • 2021-04-22
  • 1970-01-01
相关资源
最近更新 更多