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 つのメソッドに分割します。

- 別のメソッド (newMethod) の nstimer の後にコードを配置します。

-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 に答える