是否可以像这样运行 NSTimer:
NSTimer *f = [[NSTimer alloc] initWithFireDate:[NSDate date]
interval:.05
target:self
selectorselector(checkForCollisions)
userInfo:nil repeats:YES];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:f forMode: NSDefaultRunLoopMode];
[f release];
但指定我希望它运行的次数?所以,如果我希望它只运行 2 秒(假设间隔为 0.05),我可以指定它运行 40 次吗?这可能吗?
YourViewController.h
NSTimer *f;
NSInteger counter;
YourViewController.m
- (void)setup {
// initialization code
counter = 0;
f = [[NSTimer alloc] initWithFireDate:[NSDate date]
interval:.05
target:self
selectorselector(checkForCollisions)
userInfo:nil repeats:YES];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:f forMode: NSDefaultRunLoopMode];
}
- (void)checkForCollisions {
// handle check
counter++;
if (counter >= 40) {
[f invalidate];
[f release];
}
}
关于ios - 运行 NSTimer 一定次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8116758/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |