CABasicAnimation 在 segue 后不起作用

CABasicAnimation doesn't work after a segue

我正在学习一个迷宫教程 (link),在该教程中,我使用以下代码为名为 ghost1 的 UIImageView 设置了动画(该代码位于文件 ViewController.h 的 viewDidLoad: 方法中):

CGPoint origin1 = self.ghost1.center;
CGPoint target1 = CGPointMake(self.ghost1.center.x, self.ghost1.center.y-124);

CABasicAnimation *bounce1 = [CABasicAnimation         

animationWithKeyPath:@"position.y"];
bounce1.fromValue = [NSNumber numberWithInt:origin1.y];
bounce1.toValue = [NSNumber numberWithInt:target1.y];
bounce1.duration = 2;
bounce1.autoreverses = YES;
bounce1.repeatCount = HUGE_VALF;

[self.ghost1.layer addAnimation:bounce1 forKey:@"position"];

此时它运行良好。现在,我想向应用程序添加一个新的初始视图(以便首先显示菜单而不是直接显示游戏)并在我在这个新视图中按下 "start" 按钮时启动游戏。

我添加了按钮和 "show" 类型的 segue。当我按下按钮时, 可以显示游戏但是动画不工作

你知道为什么以及我该如何纠正吗?

好的,我刚刚在这里找到了解决方案:link

我只需要将代码从 viewDidLoad: 移动到 viewDidAppear:。我不完全明白为什么,但它有效。