0
我在另一个函数中有一个NSTimer,但我希望能够等待NSTimer在继续执行代码之前失效,是否有办法做到这一点?如何让函数暂停,直到NSTimer选择器完成?
- (IBAction)addLifePoints:(UIButton *)sender
{
[[ARCHDuelCalculator sharedARCHDuelCalculator] setLifePointDelta:[NSNumber numberWithInt: [self.hiddenTextField.text intValue]]];
[[ARCHDuelCalculator sharedARCHDuelCalculator] setAddOrSubstract: YES];
[[ARCHDuelCalculator sharedARCHDuelCalculator] applyingDeltaToLifePointsByDelta];
// This will animate the life points
animationTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(animateLifePoints) userInfo:nil repeats:YES];
// This is where we bring it back to the view controller
self.duelistOneLifePoints.text = [[ARCHDuelCalculator sharedARCHDuelCalculator].duelistOneLifePoints stringValue];
self.duelistTwoLifePoints.text = [[ARCHDuelCalculator sharedARCHDuelCalculator].duelistTwoLifePoints stringValue];
self.hiddenTextField.text = @"";
[self syncTextField: self.hiddenTextField];
[self.hiddenTextField resignFirstResponder];
}
- (void) animateLifePoints
{
NSNumber *sections = [[ARCHDuelCalculator sharedARCHDuelCalculator] getLifePointSections];
for(int timer = 0; timer < 100; ++timer)
{
self.duelistOneLifePoints.text = [[[ARCHUtilities sharedARCHUtilities] subtractTwoNSNumbersByDataType:@"int" firstNumber:[NSNumber numberWithInt: [self.duelistOneLifePoints.text intValue]] secondNumber:sections] stringValue];
if ((timer % 14) == 0)
{
[self playLifePointSound:@"mainLifePointSound" typeOfFile:@"mp3"];
}
}
[animationTimer invalidate];
}
你必须让代码无效的定时器启动另一个动作。 –
恐怕我不明白 –
我对代码感到困惑:你创建一个重复的定时器,让它只发一次,然后立即取消它。这是否准确?如果是这样,为什么明确地创建一个计时器呢?即使你想要,为什么创建一个重复计时器,如果你不想重复? – Tommy