0

我在另一个函数中有一个 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];
}
4

1 回答 1

1

希望这有效:

- 将 addLifePoints 拆分为 2 个方法。

-将代码放在另一个方法中的 nstimer 之后(newMethod)

- 在 nstimer 失效后立即调用 newMethod。

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

    - (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];
     [self newMethod];  //////////////////// ADD THIS LINE ALSO, continue code
   }

    -(void)newMethod{
       //...so continue code...
       // 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];
    }
于 2013-09-24T04:09:15.717 回答