iOS如何启动和暂停计时器

时间:2015-10-19 09:44:02

标签: ios objective-c

我开发了一个应用程序。在我的应用程序中,有两个UIButtons,[utf8]StartBtn,我也使用StopBtn

现在,我想在用户点击NSTimer时启动NSTimer,并在点击StartBtn时停止。

我想将StopBtn设置为1分钟,如果我在30秒内停止计时器。然后剩下30秒。再次点击NSTimer时将再次启动,完成1分钟后,它将显示alertMessage。

我知道NSTimer被[MyTimerName invalidate]停止了;方法,但我不知道如何暂停它,并再次从最后一次停止时间开始?

6 个答案:

答案 0 :(得分:2)

NSTimer中没有名为pause的事件

要实现暂停和恢复功能,请执行以下操作。

  1. 单击“开始”时,请注意当前时间。

  2. 单击暂停时,请注意当前时间。

  3. 再次点击“开始”时,您将创建NSTimer,剩余时间从第2步开始 - 第1步。

  4. 希望你知道需要做些什么......

答案 1 :(得分:1)

- (IBAction)btn_start:(id)sender
{
    if ([startpause.titleLabel.text isEqualToString:@"Start"])
{
    [startpause setTitle:@"Pause" forState:UIControlStateNormal];
    [startpause setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    if (!_myTimer)
    {
        _myTimer = [self createTimer];
    }
    [self.viewww setHidden:NO];
    [self.shuffle_out setUserInteractionEnabled:YES];
    [self.viewww setUserInteractionEnabled:YES];
}
else if ([startpause.titleLabel.text isEqualToString:@"Pause"])
{
    [startpause setTitle:@"Start" forState:UIControlStateNormal];
    [startpause setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    if (!_currentTimeInSeconds)
    {
        _currentTimeInSeconds = 0 ;
    }
    [_myTimer invalidate];
    [self.shuffle_out setUserInteractionEnabled:NO];
    [self.viewww setHidden:YES];
    [self.viewww setUserInteractionEnabled:NO];
}
}

- (NSTimer *)createTimer
{
    return [NSTimer scheduledTimerWithTimeInterval:1.0
                                        target:self
                                      selector:@selector(timerTicked:)
                                      userInfo:nil
                                       repeats:YES];
}
- (NSString *)formattedTime:(int)totalSeconds
{
    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;

    return [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
}
- (void)timerTicked:(NSTimer *)timer
{
    _currentTimeInSeconds++;
    self.lbl_timer.text = [self formattedTime:_currentTimeInSeconds];
    if ([lbl_timer.text isEqualToString:@"01:00"])
{
    [_myTimer invalidate];
    //lbl_timer.text=@"00:00";
    [startpause setTitle:@"Start" forState:UIControlStateNormal];
    [startpause setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:nil message:@"⏰...Game Over...⌛️" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
    [alrt show];
   }
}

这对我来说很有魅力......

答案 2 :(得分:0)

创建对象..

NSTimer *timer;

停止计时器

 if (timer)
    {
        [timer invalidate];
        timer = nil;
    }

启动计时器

timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(percentageUpdate) userInfo:nil repeats:YES];

您还可以在viewDidDisappear方法

中取消
- (void) viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    if (timer)
    {
        [timer invalidate];
        timer = nil;
    }    
}

答案 3 :(得分:0)

使无效并重新创建NSTimer对象的唯一方法

答案 4 :(得分:0)

没有直接的方法可以做到这一点。您可以通过以下方式实现此目的:

步骤1:创建用于保存计时器的属性,计数计时器和计数器以计算已用时间

@property (nonatomic, assign) NSInteger timerCount;
@property (nonatomic, strong) NSTimer *myCountTimer;
@property (nonatomic, strong) NSTimer *myTimer;

第2步:启动计时器,计时器并实现计时器处理程序:

- (void)startTimer {
    self.myCountTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self  selector:@selector(countTimer:) userInfo:nil repeats:NO];

    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:60.0f target:self  selector:@selector(fireTimer:) userInfo:nil repeats:NO];
}


- (void)countTimer:(NSTimer *)inTimer {
    self.timerCount += 1;
}


- (void)fireTimer:(NSTimer *)inTimer {
    // Timer is fired.
}

第3步:实施播放/暂停计时器:

- (void)pauseTimer {
    [self.myTimer invalidate];
    self.myTimer = nil;
}

- (void)resumeTimer {
    if(self.myTimer) {
        [self.myTimer invalidate];
        self.myTimer = nil;
    }

    NSInteger remainingTime = 60 - self.timerCount;

    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:remainingTime target:self  selector:@selector(fireTimer:) userInfo:nil repeats:YES];
}

答案 5 :(得分:0)

在Objective-C中 将两个全局变量声明为

int time;
NSTimer *timer;

- (IBAction)start:(id)sender
 {
   timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self  selector:@selector(increaseTimer) userInfo:nil repeats:YES];
 }

- (IBAction)pause:(id)sender
{
  [timer invalidate];
}
- (IBAction)reset:(id)sender {
     [timer invalidate];
     time = 0;
     _lbl_tmer.text=@"0";
 }
  -(void)increaseTimer {
   time++;
    NSString *str=[NSString stringWithFormat:@"%d",time];
    _lbl_tmer.text = str;
 }

在Swift中

声明两个全局var

var timer = NSTimer()

var time = 0

从此相应地操纵它们

@IBOutlet var timerLabel: UILabel!

func increaseTimer() {

    time++

    timerLabel.text = "\(time)"

}

@IBAction func play(sender: AnyObject) {

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("increaseTimer"), userInfo: nil, repeats: true)

}

@IBAction func pause(sender: AnyObject) {

    timer.invalidate()

}

@IBAction func reset(sender: AnyObject) {

    timer.invalidate()

    time = 0

    timerLabel.text = "0"

}

快乐编码.. :)