新视图控制器被推入堆栈后,UIViewanimation继续运行

解决了。 可笑的尴尬。 我没有使用实例variables,我在实现中将它声明为花括号。

真的很挣扎。 我有一个视图控制器pipe理一个animation倒数计时器。 animation是使用recursion的UIViewanimation和一个简单的int ivar减less。 像下面这样(这是一个简化的片段):

- (void) animate { [UIView animateWithDuration:0.75f delay:0.25f options:UIViewAnimationCurveEaseInOut animations:^{ aView.center = newCenter; counter--; } completion:^(BOOL finished) { if (counter != 0) [self animate]; }]; } 

在倒计时期间,用户可以轻扫以进入下一步,而不是等待计时器完成倒计时。 当用户刷新一个新的视图控制器 – 当前视图控制器的一个新实例 – 被压入堆栈,并使用下一步的值:

 TimerViewController * vc = (TimerViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"Timer"]; [vc setTask: _task + 1]; [self.navigationController pushViewController:vc animated:YES]; 

新的视图控制器滑动到位并开始倒计时,但前一个视图控制器继续animation,在debugging器中,我可以清楚地看到这个视图控制器的另一个实例,我将popup一个回到,仍然运行animation,导致一个视图移动不正常。 两个不同的视图控制器实例是animation相同的aView实例?

如何停止堆栈中前一个视图控制器的animation? 我设置counter = 0viewWillDisappear和其他各种绝望的事情无济于事,我想念什么真的在这里玩。

真的很感谢任何帮助。

更新1 (添加viewWillDisappear实现):

 -(void) viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; counter = 0; } 

更新2(编辑显示实例)

我证实了这一点,有多个TimerViewController实例与自己的aView实例。 我在animations块中添加了日志logging,以显示当前的实例,任务和计数器值。

 2012-11-03 10:28:19.930 develop.[4362:907] [0x1f5a38f0]NiceTimer:viewWillAppear 2012-11-03 10:28:19.940 develop.[4362:907] [0x1f5a38f0](0)>03:00 2012-11-03 10:28:21.149 develop.[4362:907] [0x1f5a38f0](0)>02:59 2012-11-03 10:28:22.161 develop.[4362:907] [0x1f5a38f0](0)>02:58 2012-11-03 10:28:23.170 develop.[4362:907] [0x1f5a38f0](0)>02:57 2012-11-03 10:28:24.183 develop.[4362:907] [0x1f5a38f0](0)>02:56 2012-11-03 10:28:25.210 develop.[4362:907] [0x1f5a38f0](0)>02:55 2012-11-03 10:28:26.221 develop.[4362:907] [0x1f5a38f0](0)>02:54 2012-11-03 10:28:26.694 develop.[4362:907] ---swipe--- 2012-11-03 10:28:26.698 develop.[4362:907] delegate:niceTimerViewControllerDidSwipe:duringTask 2012-11-03 10:28:26.713 develop.[4362:907] [0x1f5a38f0]NiceTimer:viewWillDisappear 2012-11-03 10:28:26.715 develop.[4362:907] [0x1f5b0ac0]NiceTimer:viewWillAppear 2012-11-03 10:28:26.720 develop.[4362:907] [0x1f5b0ac0](1)>02:00 2012-11-03 10:28:27.096 develop.[4362:907] [0x1f5a38f0]NiceTimer:viewDidDisappear 2012-11-03 10:28:27.099 develop.[4362:907] [0x1f5a38f0](0)>01:59 2012-11-03 10:28:27.748 develop.[4362:907] [0x1f5b0ac0](1)>01:58 2012-11-03 10:28:27.754 develop.[4362:907] [0x1f5a38f0](0)>01:57 2012-11-03 10:28:28.754 develop.[4362:907] [0x1f5b0ac0](1)>01:56 2012-11-03 10:28:28.763 develop.[4362:907] [0x1f5a38f0](0)>01:55 2012-11-03 10:28:29.764 develop.[4362:907] [0x1f5b0ac0](1)>01:54 2012-11-03 10:28:29.770 develop.[4362:907] [0x1f5a38f0](0)>01:53 2012-11-03 10:28:30.542 develop.[4362:907] ---swipe--- 2012-11-03 10:28:30.545 develop.[4362:907] delegate:niceTimerViewControllerDidSwipe:duringTask:1 2012-11-03 10:28:30.560 develop.[4362:907] [0x1f5b0ac0]NiceTimer:viewWillDisappear 2012-11-03 10:28:30.562 develop.[4362:907] [0x1f5ac3d0]NiceTimer:viewWillAppear 2012-11-03 10:28:30.567 develop.[4362:907] [0x1f5ac3d0](2)>05:00 2012-11-03 10:28:30.593 develop.[4362:907] [0x1f5a38f0](0)>04:59 2012-11-03 10:28:30.601 develop.[4362:907] [0x1f5a38f0](0)>04:58 2012-11-03 10:28:30.617 develop.[4362:907] [0x1f5a38f0](0)>04:57 2012-11-03 10:28:30.620 develop.[4362:907] [0x1f5a38f0](0)>04:56 

抱歉这么久,但这确实显示了正在发生的事情。 刷卡后,有推(代码在顶部的post)。 我期望的任务增加(0)>去(1)>并保持不变,直到下一次刷卡。 相反,我看到堆栈中的每个先前的实例继续触发,使用当前计数器的值。 每个以前的实例的animation继续对现有的计数器

更新3

我实现了一个在刷卡处理程序中被调用的委托,TVC1是在被推送时被传递给TVC2等的委托。 委托设置为0,称为removeAllAnimations等,并没有效果。 我真的认为这将做到这一点。 在这一点上,我认为我正在走错方向,试图以这种方式来解决这个问题。 做了一个新的项目,简单的实现,以确保它不是在应用程序中的其他东西。

 -(void)timerViewControllerDidSwipe:(TimerViewController *)controller duringTask:(int)task { NSLog(@"timerViewControllerDidSwipe:duringTask:%d", task); counter = 0; [self.aView.layer removeAllAnimations]; [self.view.layer removeAllAnimations]; aView = nil; // This is showing everything I tried } 

更新4

我发现这个问题! 所有实例都引用相同的int。 我不知道这是怎么可能的,整数不是特定于一个类的实例?

 2012-11-03 13:16:49.336 develop.[4656:907] [animate:0x1ed8c790](1)>02:46 2012-11-03 13:16:49.340 develop.[4656:907] **[^anim:0x1ed8c790](1)>166(duration=0x6a748)** 2012-11-03 13:16:49.344 develop.[4656:907] [^after:0x1eddb8c0](0)>166 2012-11-03 13:16:49.352 develop.[4656:907] [animate:0x1eddb8c0](0)>02:45 2012-11-03 13:16:49.356 develop.[4656:907] **[^anim:0x1eddb8c0](0)>165(duration=0x6a748)** 

在将TimerViewController推入导航堆栈之前,请尝试删除所有正在运行的animation:

 [aView.layer removeAllAnimations]; TimerViewController * vc = (TimerViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"Timer"]; [vc setTask: _task + 1]; [self.navigationController pushViewController:vc animated:YES]; 

另外,确保你已经导入了<QuartzCore/QuartzCore.h>否则你会得到下面的错误: Receiver type 'CALayer' for instance message is a forward declaration

固定。 不能更尴尬。 基本的语法问题。 我没有把大括号内的variables声明括起来。 我做到了这一点:

 @implementation TimerViewController int counter; - (void) aMethod { } @end 

而不是这个:

 @implementation TimerViewController { int counter; } - (void) aMethod { } @end 

我希望我的错误会帮助别人! 它从来不会发生在我身上。 一个错误,我不会再做。