【问题标题】:Timer count throughout the game in Objective cObjective c 中整个游戏的计时器计数
【发布时间】:2018-09-19 17:48:48
【问题描述】:

我正在创建一个游戏应用程序,我需要在目标 c 的整个应用程序屏幕中设置 2 分钟的计时器。

我在 viewDidLoad 中创建它,但每次加载视图时它都会创建一个新实例。

这是我正在使用的代码:

@interface SomeViewController ()
{
    int timerCounter;
     NSTimer *timer;   
}
@property (strong, nonatomic) IBOutlet UILabel *timerLbl;

@end

@implementation SomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self startCountdown];
}


- (void)startCountdown
{
    timer = [NSTimer scheduledTimerWithTimeInterval:1
                                             target:self
                                           selector:@selector(countdownTimer:)
                                           userInfo:nil
                                            repeats:YES];
}

- (void)countdownTimer:(NSTimer *)timer
{
    timerCounter--;

    int minutes = timerCounter / 60;
    int seconds = timerCounter % 60;

    NSString *string = [NSString stringWithFormat:@"%02d", minutes];
    NSString *string2 = [NSString stringWithFormat:@"%02d", seconds];

    NSString *timeTotal = [string stringByAppendingString:@":"];
    NSString *timeTotal2 = [timeTotal stringByAppendingString:string2];

    _timerLbl.text = timeTotal2;
    if (timerCounter <= 0) {
        [timer invalidate];   
    }
}

【问题讨论】:

    标签: ios objective-c nstimer


    【解决方案1】:

    您需要在 VC 释放时使其失效

    - (void)dealloc {
        [timer invalidate];
    }
    

    //

    第二个 VC 可能是这个样子

    #import "ggViewController.h"
    NSInteger timerCounter = 120;     // declared global to hold the value 
    @interface ggViewController ()
    {
        NSTimer*timer;
    }
    @end
    
    @implementation ggViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    
       // instead of using selector use this inline callback
      timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:true block:^(NSTimer * timer) {
    
         timerCounter--;
    
         int minutes = timerCounter / 60;
         int seconds = timerCounter % 60;
    
         NSString *string = [NSString stringWithFormat:@"%02d", minutes];
         NSString *string2 = [NSString stringWithFormat:@"%02d", seconds];
    
         NSString *timeTotal = [string stringByAppendingString:@":"];
         NSString *timeTotal2 = [timeTotal stringByAppendingString:string2];
    
         _timerLbl.text = timeTotal2;
          if (timerCounter <= 0) {
              [timer invalidate];   
             }
        }];
    
    }
    
    -(void)viewDidDisappear:(BOOL)animated{
    
        [super viewDidDisappear:animated];
    
        [timer invalidate];
    }
    
    
    - (IBAction)gg:(id)sender {
    
        [self dismissViewControllerAnimated:true completion:nil];
    }
    @end
    

    【讨论】:

    • 即使我回到同一个屏幕,我也需要它运行
    • 然后使用 dealloc
    • 让我解释一下场景,我在游戏开始时加了一个 2 分钟的计时器,我在 10 秒内回答一个问题,现在当我回到同一个屏幕时,计时器不应该失效(它必须一直持续到它变成 0)
    • 这会在vc即将发布时使定时器失效,先试试
    • 使用过,但它再次将计时器刷新到 2:00
    猜你喜欢
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2017-02-18
    • 2017-04-11
    • 2020-08-05
    • 1970-01-01
    相关资源
    最近更新 更多