我怎样才能制作一个计数器?

发布于 2024-11-02 20:46:07 字数 32 浏览 0 评论 0原文

如何制作一个从 0 到 10 的计数器并选择延迟?

How can I make a counter from 0 to 10 choosing the delay?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

帅的被狗咬 2024-11-09 20:46:07

您需要使用NSTimer

请检查以下代码作为参考。

- (void) startCounter {
     counterCount = 0;
    [NSTimer scheduledTimerWithInterval:0.09f target:self selector:@selector(showElapsedTime:) userInfo:nil repeats:YES];
}

showElapsedTime 将在您提供的延迟后调用。

-(void) showElapsedTime: (NSTimer *) timer {
    counterCount++;
    if(counterCount == 10)
    {
      [timer invalidate];
    }

// Write your code here 
}

you need to use NSTimer,

Check the below code as reference.

- (void) startCounter {
     counterCount = 0;
    [NSTimer scheduledTimerWithInterval:0.09f target:self selector:@selector(showElapsedTime:) userInfo:nil repeats:YES];
}

showElapsedTime will be called after delay, you provide.

-(void) showElapsedTime: (NSTimer *) timer {
    counterCount++;
    if(counterCount == 10)
    {
      [timer invalidate];
    }

// Write your code here 
}
十年九夏 2024-11-09 20:46:07

创建一个 NSTimer:

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds 
                                     target:(id)target 
                                   selector:(SEL)aSelector 
                                   userInfo:(id)userInfo 
                                    repeats:(BOOL)repeats;

您的计时器可能如下所示:

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

在调用的方法中,增加一个计数器,并检查是否已达到计数以停止计时器:

- (void)timerFired:(NSTimer *)timer
{
     static int counter = 0;

     // Do Something

     counter++;
     if(counter == 10)
          [timer invalidate];
}

Create an NSTimer:

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds 
                                     target:(id)target 
                                   selector:(SEL)aSelector 
                                   userInfo:(id)userInfo 
                                    repeats:(BOOL)repeats;

Yours would probably look like this:

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

In the method that is invoked, increment a counter, and check to see if you've hit your count to stop the timer:

- (void)timerFired:(NSTimer *)timer
{
     static int counter = 0;

     // Do Something

     counter++;
     if(counter == 10)
          [timer invalidate];
}
删除会话 2024-11-09 20:46:07

NSObject类的方法[performSelector: withObject: afterDelay]通常用于定时操作。

The method [performSelector: withObject: afterDelay] of the NSObject class is normally used for timed operations.

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