2015-10-19 112 views
2

我开发了一个应用程序。在我的应用程序中,有两个UIButtons,StartBtnStopBtn,并且我还使用NSTimeriOS如何启动和暂停计时器

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

我想设置NSTimer 1分钟,如果我在30秒内停止计时器。然后保持30秒。将再次启动,当我再次点击StartBtn,完成1分钟后,它应该显示alertMessage。我知道NSTimer是由[MyTimerName invalidate]停止的;方法,但我不知道如何暂停它,并再次从最后停止时间开始?

+0

我相信你已经重新声明它。 'MyTimerName = [NSTimer ...]'我认为你正在制作一个计时器应用程序? – Arc676

+0

我想在我的应用程序中录制一分钟内完美的语音。只剩下这个计时器问题。 –

+0

如果您有代码段,请将其发布。它真的很感激。 –

回答

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]; 
    //[email protected]"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]; 
    } 
} 

这是工作般的魅力对我来说...

+0

非常感谢...为我工作得很好 –

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; 
    }  
} 
0

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

1

没有事件的NSTimer

称为pause为了实现暂停和恢复下面的功能是你可以做什么。

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

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

  3. 当您再次单击开始时,您将创建NSTimer,其中剩余的时间为步骤2时间 - 步骤1时间。

希望你有上需要做什么样的想法...

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]; 
} 
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; 
    [email protected]"0"; 
} 
    -(void)increaseTimer { 
    time++; 
    NSString *str=[NSString stringWithFormat:@"%d",time]; 
    _lbl_tmer.text = str; 
} 

在斯威夫特

声明两个全局变量

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" 

} 

编码快乐.. :)