由 NSTimer 重复递增的静态计数器

发布于 2024-07-26 09:51:42 字数 147 浏览 4 评论 0原文

每隔 1/16 秒,我就会触发一个 NSTimer,每次都会调用一个方法。 我想创建一个静态整数,每次调用该方法时都会增加“1”,一旦静态整数等于“16”,我希望调用另一个方法并将静态整数重置为“0”。

任何见解都将受到高度赞赏。 (语言是Obj-C)

Every 1/16 of a second, I have an NSTimer that fires, calling a method each time. I want to create a static integer that is increased by '1' each time the method is called, once the static integer is equal to '16', I wish to call another method and reset the static integer to '0'.

Any insight is greatly appreciated. (Language is Obj-C)

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

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

发布评论

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

评论(1

秋日私语 2024-08-02 09:51:42

所以将其声明为静态 int...

static int myCounter;
@implementation SomeClass

- (id) init {
  if (self = [super init]) {
    myCounter = 0;
    NSTimer * someTimer = [NSTimer scheduledTimerWithTimeInterval:(1/16) target:self selector:@selector(fireTimer:) userInfo:nil repeats:YES];
  }
  return self;
}

- (void) fireTimer:(NSTimer *)aTimer {
  myCounter++;
  if (myCounter == 16) {
    [self doSomeMethod];
    myCounter = 0;
  }
}

@end

So declare it as a static int...

static int myCounter;
@implementation SomeClass

- (id) init {
  if (self = [super init]) {
    myCounter = 0;
    NSTimer * someTimer = [NSTimer scheduledTimerWithTimeInterval:(1/16) target:self selector:@selector(fireTimer:) userInfo:nil repeats:YES];
  }
  return self;
}

- (void) fireTimer:(NSTimer *)aTimer {
  myCounter++;
  if (myCounter == 16) {
    [self doSomeMethod];
    myCounter = 0;
  }
}

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