Objective c 目标C:远程使计时器失效

Objective c 目标C:远程使计时器失效,objective-c,ios,methods,timer,Objective C,Ios,Methods,Timer,当调用另一个方法时,如何使一个方法中的计时器无效?基本上,当调用tapFig时,我希望它向moveStickFig发送一条消息以使计时器无效 -(void) moveStickFig:(NSTimer *)timer { UIButton *stick = (UIButton *)timer.userInfo; CGPoint oldPosition = stick.center; stick.center = CGPointMake(oldPosition.x + 1

当调用另一个方法时,如何使一个方法中的计时器无效?基本上,当调用
tapFig
时,我希望它向
moveStickFig
发送一条消息以使计时器无效

-(void) moveStickFig:(NSTimer *)timer {
    UIButton *stick = (UIButton *)timer.userInfo;
    CGPoint oldPosition = stick.center;
    stick.center = CGPointMake(oldPosition.x + 1 , oldPosition.y);
    if (oldPosition.x == 900) {
        [stick removeFromSuperview];
        healthCount--;
        NSLog(@"%d", healthCount);
        [healthBar setImage:[UIImage imageNamed:[NSString stringWithFormat:@"health%d.png",healthCount]]];
    }
}

-(void) tapFig:(id)sender {
    UIButton *stick = (UIButton *)sender;
    count++;
    score.text = [NSString stringWithFormat:@"%d", count];
    [stick removeFromSuperview];
    [stick release];
}

我想您需要在
moveStickFig
中设置一个标志,当调用
tapFig
时,该标志将设置为true

-(void) moveStickFig:(NSTimer *)timer
{
    if( isTimerInvalidateSet )
    {
        [ self timer:invalidate ];
        return;
    }
    // ......
}

// you need to pass the same timer instance to `tapFig` that you earlier passed to `moveStickFig`.

-(void) tapFig:(id)sender
{
    isTimerInvalidateSet = true;
    [ self moveStickFig:theTimerInstance ] ; // theTimerInstance is same as earlier you passed to `moveStickFig`

    isTimerInvalidateSet = false;
    // ......
}
注意:通常,您会设置计时器,以每秒固定的帧数重复调用函数。计时器的工作就是按这个速率调用它。不需要重复传递计时器实例。如果这是你想要的,那好吧<代码>但是,如果您需要继续游戏逻辑,则需要将isTimerInvalidateSet重置为false。希望这有帮助