需要帮助配置 NSTimer 和 UIButton
我有以下秒表功能的代码。
我有 2 个 UIButton。一个用于启动/停止,另一个用于重置。
我想将这 2 个按钮组合成 1 个按钮,用作启动按钮,然后在计时器运行时,它将成为停止按钮,按下时会重置计时器。
我如何修改此代码以反映这一点?
- (IBAction)startStop:(UIButton *)sender
{
if ( self.myTimer )
{
[self.myTimer invalidate];
self.myTimer = nil;
[sender setTitle:@"Start" forState:UIControlStateNormal];
}
else
{
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
[sender setTitle:@"Stop" forState:UIControlStateNormal];
}
}
- (void)handleTimer:(NSTimer *)timer
{
self.counter--;
self.timerLabel.text = [NSString stringWithFormat:@"%ld", self.counter];
if ( self.counter <= 0 )
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
[self.myTimer invalidate];
self.myTimer = nil;
self.timerButton.enabled = NO;
}
}
- (IBAction)reset:(id)sender
{
self.timerButton.enabled = YES;
self.counter = self.counterSegment;
self.timerLabel.text = timerCount;
self.timerButton.titleLabel.text = @"Start";
self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
}
- (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index
{
if ( self.myTimer )
{
[self.myTimer invalidate];
self.myTimer = nil;
self.timerButton.titleLabel.text = @"Start";
self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
}
else
{
self.timerButton.enabled = YES;
self.counter = self.counterSegment;
self.timerLabel.text = timerCount;
self.timerButton.titleLabel.text = @"Start";
self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
}
if (index == 0)
{
NSLog(@"15 sec");
self.timerCount = @"15";
self.counterSegment = 15;
}
else if (index == 1)
{
NSLog(@"30 sec");
self.timerCount = @"30";
self.counterSegment = 30;
}
}
I have the following code for a stopwatch function.
I have 2 UIButtons. One is for start/stop, the other is for reset.
I want to combine these 2 into just 1 button, that functions as a start button, then while the timer is running, it will be a stop button, that when pushed, resets the timer.
How can I modify this code to reflect this?
- (IBAction)startStop:(UIButton *)sender
{
if ( self.myTimer )
{
[self.myTimer invalidate];
self.myTimer = nil;
[sender setTitle:@"Start" forState:UIControlStateNormal];
}
else
{
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
[sender setTitle:@"Stop" forState:UIControlStateNormal];
}
}
- (void)handleTimer:(NSTimer *)timer
{
self.counter--;
self.timerLabel.text = [NSString stringWithFormat:@"%ld", self.counter];
if ( self.counter <= 0 )
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
[self.myTimer invalidate];
self.myTimer = nil;
self.timerButton.enabled = NO;
}
}
- (IBAction)reset:(id)sender
{
self.timerButton.enabled = YES;
self.counter = self.counterSegment;
self.timerLabel.text = timerCount;
self.timerButton.titleLabel.text = @"Start";
self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
}
- (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index
{
if ( self.myTimer )
{
[self.myTimer invalidate];
self.myTimer = nil;
self.timerButton.titleLabel.text = @"Start";
self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
}
else
{
self.timerButton.enabled = YES;
self.counter = self.counterSegment;
self.timerLabel.text = timerCount;
self.timerButton.titleLabel.text = @"Start";
self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
}
if (index == 0)
{
NSLog(@"15 sec");
self.timerCount = @"15";
self.counterSegment = 15;
}
else if (index == 1)
{
NSLog(@"30 sec");
self.timerCount = @"30";
self.counterSegment = 30;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行的步骤:
从用户界面中删除重置按钮;
修改
startStop
方法,以便在计时器已运行时按下该方法,它将向自身发送reset
消息:我认为这应该足够了。看一下这段代码:
由于在
reset
中您没有使用sender
参数,因此您可以将其删除并具有:这样您将能够使用此方法以更通用的方式。
Steps you could do:
remove the reset button from you UI;
modify the
startStop
method so that when pressed while the timer is already running, it will send areset
message to itself:I think it should be enough. Have a look at this code:
Since in
reset
you are not using thesender
argument, you can remove it and have:In this way you will be able to use this method in a more generic way.