需要帮助配置 NSTimer 和 UIButton

发布于 2024-11-25 17:46:16 字数 2080 浏览 4 评论 0原文

我有以下秒表功能的代码。

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高跟鞋的旋律 2024-12-02 17:46:16

您可以执行的步骤:

  1. 从用户界面中删除重置按钮;

  2. 修改 startStop 方法,以便在计时器已运行时按下该方法,它将向自身发送 reset 消息:

我认为这应该足够了。看一下这段代码:

- (IBAction)startStop:(UIButton *)sender 
{
if ( self.myTimer ) 
{
    [self.myTimer invalidate];
    self.myTimer = nil;

    [self reset:sender];   //-- LINE ADDED

    [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];
}
}

由于在 reset 中您没有使用 sender 参数,因此您可以将其删除并具有:

- (void)reset
{
self.timerButton.enabled = YES;
self.counter = self.counterSegment;
self.timerLabel.text = timerCount;
self.timerButton.titleLabel.text = @"Start";
self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
}

这样您将能够使用此方法以更通用的方式。

Steps you could do:

  1. remove the reset button from you UI;

  2. modify the startStop method so that when pressed while the timer is already running, it will send a reset message to itself:

I think it should be enough. Have a look at this code:

- (IBAction)startStop:(UIButton *)sender 
{
if ( self.myTimer ) 
{
    [self.myTimer invalidate];
    self.myTimer = nil;

    [self reset:sender];   //-- LINE ADDED

    [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];
}
}

Since in reset you are not using the sender argument, you can remove it and have:

- (void)reset
{
self.timerButton.enabled = YES;
self.counter = self.counterSegment;
self.timerLabel.text = timerCount;
self.timerButton.titleLabel.text = @"Start";
self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
}

In this way you will be able to use this method in a more generic way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文