更改计时器的时间间隔

发布于 2024-12-06 06:41:06 字数 427 浏览 1 评论 0原文

这是我的问题: 例如,是否可以在 ViewDidLoad 中 10 秒后增加 scheduledTimerWithTimeInterval:2 例如“3”。 例如,从这个:

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

到这个:

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

谢谢 对不起我的英语我法语:/

here is my question:
Is it possible to increase the scheduledTimerWithTimeInterval:2 for example of "3" after 10 seconds in ViewDidLoad for example.
E.g., from this:

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

to this:

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

thank you
sorry for my english I french :/

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

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

发布评论

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

评论(2

因为看清所以看轻 2024-12-13 06:41:06

像这样递归地重新安排计时器:

float gap = 0.50;

[NSTimer scheduledTimerWithTimeInterval:gap target:self selector:@selector(onTimer) userInfo:nil repeats:NO];

-(void) onTimer {
    gap = gap + .05;
    [NSTimer scheduledTimerWithTimeInterval:gap target:self selector:@selector(onTimer) userInfo:nil repeats:NO];
}

========

或者根据 当我更改时间间隔的值时,如何更新我的 NSTimer

使其无效:

[myTimer invalidate];

然后使用新时间创建一个新的。您可能还必须先将其设置为零。

myTimer = nil;
myTimer = [NSTimer scheduledTimerWithTimeInterval:mySlider.value 
                                           target:self 
                                         selector:@selector(myMethod) 
                                         userInfo:nil 
                                          repeats:YES];

Reschedule the timer recursively like this:

float gap = 0.50;

[NSTimer scheduledTimerWithTimeInterval:gap target:self selector:@selector(onTimer) userInfo:nil repeats:NO];

-(void) onTimer {
    gap = gap + .05;
    [NSTimer scheduledTimerWithTimeInterval:gap target:self selector:@selector(onTimer) userInfo:nil repeats:NO];
}

========

Or according to How can I update my NSTimer as I change the value of the time interval

Invalidate it with:

[myTimer invalidate];

Then create a new one with the new time. You may have to set it to nil first as well.

myTimer = nil;
myTimer = [NSTimer scheduledTimerWithTimeInterval:mySlider.value 
                                           target:self 
                                         selector:@selector(myMethod) 
                                         userInfo:nil 
                                          repeats:YES];
榕城若虚 2024-12-13 06:41:06

使用 setFireDate: 重新安排计时器。您需要在 ivar 中跟踪计时器。例如:

@property (nonatomic, readwrite, retain) NSTimer *timer;

@synthesize timer=timer_;

- (void)setTimer:(NSTimer *)aTimer {
  if (timer_ != aTimer) {
    [aTimer retain];
    [timer_ invalidate];
    [timer_ release];
    timer_ = aTimer;
  }

- (void)dealloc {
  [timer_ invalidate];
  [timer_ release];
}

...

self.timer = [NSTimer scheduledTimerWithTimeInterval:...];

...

self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:3]; // reschedule for 3 seconds from now

Use setFireDate: to reschedule the timer. You'll need to keep track of the timer in an ivar. For example:

@property (nonatomic, readwrite, retain) NSTimer *timer;

@synthesize timer=timer_;

- (void)setTimer:(NSTimer *)aTimer {
  if (timer_ != aTimer) {
    [aTimer retain];
    [timer_ invalidate];
    [timer_ release];
    timer_ = aTimer;
  }

- (void)dealloc {
  [timer_ invalidate];
  [timer_ release];
}

...

self.timer = [NSTimer scheduledTimerWithTimeInterval:...];

...

self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:3]; // reschedule for 3 seconds from now
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文